请帮助我通过Json响应从CURL请求中填充向量或数组。
我收到OSRM网站的回复(您可以在下面看到它)/这是JSON格式,但是我无法解析它并稍后在我的代码中使用。我不知道该怎么做。 谁能帮我吗?
{“ tracepoints”:
[
{“ location”:[73.103166,49.817488],“ name”:“ some street1”,“ hint”:“ 10UDgP ___ k =”,“ matchings_index”:0,“ waypoint_index”:0,“ alternatives_count”:0}, {“ location”:[73.104389,49.817199],“ name”:“ some street2”,“提示”:“ xUUDgP ___ k =”,“ matchings_index”:0,“ waypoint_index”:1,“ alternatives_count”:0}, {“位置”:[73.108378,49.815211],“名称”:“一些street3”,“提示”:“-EUDgP ___ k =”,“ matchings_index”:0,“ waypoint_index”:2,“ alternatives_count”:0} ],“匹配项”:[{ “距离”:480.5,“持续时间”:31.6,“重量”:31.6,“ weight_name”:“可路由性”,“几何形状”:“ i〜`oHy} d} Lx @ sF | A_K | @gFpFuD”,“信心” “:0.579856,”腿“: [{“距离”:93.5,“持续时间”:6.2,“重量”:6.2,“摘要”:“”,“步数”:[]},{“距离”:387,“持续时间”:25.4,“重量“:25.4,”摘要“:”“,”步骤“:[]}] } ], “ code”:“确定”}
我下载了https://github.com/nlohmann/json,但无法使用。 请给我一些解决问题的方法。 我的VS2015代码如下
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "wldap32.lib")
#pragma comment(lib, "Normaliz.lib")
#pragma comment(lib, "crypt32.lib")
#define CURL_STATICLIB
#ifdef _DEBUG
# pragma comment (lib, "curl/libcurl_a_Debug.lib")
# pragma comment (lib, "curl/libssl_Debug.lib")
# pragma comment (lib, "curl/libcrypto_Debug.lib")
#else
# pragma comment (lib, "curl/libcurl_a_Release.lib")
#endif // DEBUG _DEBUG
#include "curl\curl.h"
#include "stdafx.h"
#include "iostream"
#include "string"
#include <iomanip>
#include "nlohmann/json.hpp"
using json = nlohmann::json;
static int writer(char *data, size_t size, size_t nmemb, std::string *writerData)
{
if (writerData == NULL)
return 0;
writerData->append(data, size*nmemb);
return size * nmemb;
}
int main()
{
json JStruct ;
std::string content;
//std::cout << "Some_Text";
std::vector<double> myLatpoints;
std::vector<double> myLonpoints;
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = nullptr;
curl = curl_easy_init();
curl_global_cleanup();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:5000/match/v1/driving/73.103130,49.817423;73.104353,49.817137;73.10824,49.81517");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
CURLcode code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
//curl_global_cleanup();
JStruct = content;
// HELP ME TO FILL UP MY VECTORS HERE PLEASE
std::cin.get() ;
return 0;
}