我正在寻找一个易于理解的库,可以用C++发出HTTP REST请求,然后我发现了CPR。我成功地从服务器获得了响应,但我发现很难访问返回的JSON对象。
接口Get请求:
auto r = cpr::Get(cpr::Url{ "https://example.net/api/token" },
cpr::Parameters{ {"username", login}, {"password", password},
{"hwid", "TestChecker"}, {"obt", "1"}});
r.status_code;
r.header["application/json"];
r.text;
我试图将r.text
传递给nlohmann::json j = r.text;
并像预期的那样访问我想要的特定对象string xx = j["token"];
,但它抛出了一个错误。
如果有人能告诉我如何实现我没能做到的事情,我将不胜感激。
编辑:添加引用
CPR:https://www.codeproject.com/Articles/1244632/Making-HTTP-REST-Request-in-Cplusplus
nlohmann/json:https://github.com/nlohmann/json
发布于 2019-05-16 03:38:11
我确实对代码进行了一些尝试,最终弄清楚了。基本上,我想要做的是将"JSON字符串“转换为JSON对象。我是通过使用nlohmann::json::parse();
方法实现的
Json j = Json::parse(r.text);
string xx = j["token"];
https://stackoverflow.com/questions/56155428
复制相似问题