我有这段代码:
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()
的数据返回?
谢谢。
答案 0 :(得分:1)
您无需致电serialize
来访问json值。一旦你有一个json::value
,它拥有一个json对象,你可以遍历它以获得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。