{ "areaList": [ { "ATC_status": 3, }, { "ATC_status": 4, }], "baseCapsuleId": "1659417448280555521", "taggerName": "liangshimin" } c++ json解析
时间: 2023-07-05 21:27:14 浏览: 106
C++ Json解析
5星 · 资源好评率100%
你可以使用现成的 JSON 库来解析 JSON 字符串。以下是使用 RapidJSON 库解析该 JSON 字符串的示例代码:
```c++
#include <iostream>
#include "rapidjson/document.h"
using namespace rapidjson;
int main() {
const char* json = "{ \"areaList\": [ { \"ATC_status\": 3, }, { \"ATC_status\": 4, }], \"baseCapsuleId\": \"1659417448280555521\", \"taggerName\": \"liangshimin\" }";
Document doc;
doc.Parse(json);
// 获取 areaList 数组
const Value& areaList = doc["areaList"];
assert(areaList.IsArray());
// 遍历 areaList 数组
for (Value::ConstValueIterator it = areaList.Begin(); it != areaList.End(); ++it) {
// 获取 ATC_status 字段
const Value& ATC_status = (*it)["ATC_status"];
assert(ATC_status.IsInt());
std::cout << ATC_status.GetInt() << std::endl;
}
// 获取 baseCapsuleId 字段
const Value& baseCapsuleId = doc["baseCapsuleId"];
assert(baseCapsuleId.IsString());
std::cout << baseCapsuleId.GetString() << std::endl;
// 获取 taggerName 字段
const Value& taggerName = doc["taggerName"];
assert(taggerName.IsString());
std::cout << taggerName.GetString() << std::endl;
return 0;
}
```
运行结果:
```
3
4
1659417448280555521
liangshimin
```
阅读全文