1

我有这个代码:

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()

谢谢你。

4

1 回答 1

2

您无需调用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;

一切都在这里得到很好的解释。

于 2018-04-20T07:23:15.747 回答