我需要从CPP REST服务解析JSON响应。
如果响应为成功,则服务器发送以下响应:
BODY
{
"data":{
"resourceID":"4567890",
"chunkSize":"10"
}
"error": null
}
如果有错误,则发送以下JSON
{
"error": {
"errorCode": "123",
"errorMessage": "Not able to process the request"
}
}
我尝试了代码:
return client.request(request).then([](http_response response) -> pplx::task<json::value>
{
std::wostringstream ss;
ss << L"Server returned returned status code " << response.status_code() << L"." << std::endl;
std::wcout << ss.str();
//return pplx::task_from_result(json::value());
return response.extract_json();
})
.then([](pplx::task<json::value> previousTask)
{
try
{
const json::value& v = previousTask.get();
// Perform actions here to process the JSON value...
std::wcout << "Value: " << v.serialize() << endl;
}
catch (const http_exception& e)
{
// Print error.
wostringstream ss;
ss << e.what() << endl;
wcout << ss.str();
}
});
但是我无法解析每个项目并在JSON响应中获取错误代码或必需的变量。