我有以下代码:
wcout << jvalue.serialize() << endl;
打印出整个json数据:
{
"data": {
"timestamp": [
{
"_id": "5ad93fc48084e1089cd9667b",
"staff_id": 172,
"staff_name": "Michael Edano",
"time_in": "2018-04-20T01:17:56.787Z",
"time_out": null,
"status": ['Leave','Vacation','Absent'],
"createdAt": "2018-04-20T01:17:56.790Z",
"updatedAt": "2018-04-20T01:17:56.790Z",
"__v": 0
}
],
"success": true
}
}
有人可以给我一个例子,让我说
_id
字段和
status[]
字段(是数组)的单个数据从
web::json::value::serialize()
返回的数据?谢谢。
最佳答案
您无需调用serialize
即可访问json值。一旦拥有一个jsont对象的json::value
,您就可以遍历它以获得json::value
的内部对象和数组:
json::value jvalue; //this is your initial value
// this new value will hold the whole 'data' object:
json::value data = jvalue.at(U("data"));
// read `timestamp` array from `data` and directly read
// the item at index 0 from it:
json::value timestamp = data.at(U("timestamp")).at(0);
// from `timestamp` (first item of timestmap array`)
json::value id = timestamp.at(U("_id"));
// print `id` as string
std::wcout << id.as_string() << std::endl;
// read `status` array first item and print it as string
json::value status = timestamp.at(U("status"));
std::wcout << status.at(0).as_string() << std::endl;
您可以从上面的代码中猜出,可以将对
at
的调用链接在一起:// read the `_id` value at once and print it as string
std::wcout << jvalue.at(U("data")).at(U("timestamp")).at(0).at(U("_id")).as_string() << std::endl;
一切都很好地解释了here。
关于c++ - 如何使用cpprestsdk/casablanca提取从web::json::value::serialize()返回的特定数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49933541/