如何使用 cpprestsdk/casablanca 提取从 web::json::value::serialize() 返回的特定数据

How to extract specific data returned from web::json::value::serialize() with cpprestsdk/casablanca

我有这个代码:

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
    }
}

谁能给我一个例子,说明如何让 let say _id 字段 和 status[] 字段的单个数据(数组)

来自数据 return web::json::value::serialize() ?

谢谢。

您不必调用 serialize 来访问 json 值。一旦你有了一个 json::value,它包含一个 json 对象,你可以遍历它来获取内部对象和数组,如 json::value's:

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