【发布时间】:2021-07-30 17:40:21
【问题描述】:
我需要读取 JSON 文件中的一个字段,该文件本身就是一个 JSON。我需要一口气读取 JSON 字段。有什么办法吗?下面提供了我正在尝试阅读的示例 JSON。
enter code here
{
"responses": [
{
"id": "1",
"status": 200,
"headers": {
"OData-Version": "4.0",
"Content-Type":"application/json;odata.metadata=minimal;odata.streaming=true"
},
"body": {
"createdDateTime": "2021-04-22T09:24:59.394Z",
"displayName": "Test1",
"visibility": "public",
"isMembershipLimitedToOwners": false,
"discoverySettings": { "showInTeamsSearchAndSuggestions": true },
"memberSettings": {
"allowCreateUpdateChannels": true,
"allowCreateUpdateRemoveConnectors": true
},
"guestSettings": {
"allowCreateUpdateChannels": true,
"allowDeleteChannels": false
},
"messagingSettings": {
"allowUserEditMessages": true,
"allowChannelMentions": true
},
"funSettings": {
"allowGiphy": true,
"allowCustomMemes": true
}
} } ]
}
我正在尝试使用下面的代码读取“body”字段(在 boost::property_tree::ptree jsonBatchResponse jsonBatchResponse 中读取 json)。但是 strBody 是空的,它没有正确读取“Body”字段。 :
enter code here
for (auto& v : jsonBatchResponse.get_child("responses"))
{
std::string strID = v.second.get<std::string>("id", "");
std::string strStatus = v.second.get<std::string>("status", "");
std::string strBody = v.second.get<std::string>("body", "");
}
看起来 v.second.getstd::string("body", "") 不是读取 JSON 字段的正确方法。有没有其他可用的方法(除了读取 JSON 值中的各个字段)?请告诉我。
【问题讨论】:
标签: c++ json boost boost-propertytree