RESTful API, C++restSDK, JSON in C++

RESTful API, C++restSDK, JSON in C++

本文关键字:in C++ JSON C++restSDK API RESTful      更新时间:2023-10-16

我正在尝试使用此RESTFUL API:https://github.com/binance-exchange/binance-incial-api-api-docs/blob/master/master/rest-api.md

为此,我正在使用此C SDK:https://github.com/microsoft/cpprestsdk

Binance API返回所有内容,如JSON,这是我认为我的问题所在的地方。

我的代码在这里:https://pastebin.com/sebaxva0

我认为错误在此功能中(在Pastebin中也注释(:

void print_test(json::value const & value){
    if(!value.is_null()){
        //I am doing something wrong here I'm pretty sure.
        auto response = value[L"responseData"];
       //"responseData" is probably not what should be here I think?
        auto results = response[L"serverTime"];
        wcout << results.as_integer() << endl;
    }
} 

基本上,我试图通过从Binance API为服务器时间进行简单的GET方法来测试此API。根据二元文档:

Check server time
GET /api/v1/time
Test connectivity to the Rest API and get the current server time.
Weight: 1
Parameters: NONE
Response:
{
  "serverTime": 1499827319559
}

因此,我想对该JSON提出一个请求,然后让我的C 程序输出Servertime的值。当我尝试按原样运行代码时,我会发现一个错误说:

error: no viable overloaded operator[] for type
  'const json::value'
    auto response = value[L"responseData"];

但是,我正在跟随C RESTSDK的示例,此处找到:http://mariusbancila.ro/blog/2013/08/02/cpp-rest-sdk-in-visual-studio-2013/

我认为我的问题与我在Pastebin上的第45行所作的评论下的两行有关。据推测,我将错误的值分配给该行下的一个或两个变量。我知道这是一个非常具体的帮助请求,但是有人知道我在做什么错以及如何使Servertime的价值显示?

我在大量挖掘后自己解决了这个问题:)错误是在此函数中,现在已使用此代码固定:

void print_test(json::value const & value){
    if(!value.is_null()){
        json::value test = value;
        cout << test["serverTime"] << endl;
    }
}