"enumList": [{ "key": "zoom", "value": "zoom", "alias": "zoom" }, { "key": "tencent", "value": "tencent", "alias": "腾讯" }, { "key": "huaweiCloud", "value": "huaweiCloud", "alias": "华为" }, { "key": "null", "value": "null", "alias": "无" }, { "key": "feishuPersonal", "value": "feishuPersonal", "alias": "飞书个人" }],比如我有上述格式的JSON数据,我如何用machina.io中庸C++解析
时间: 2024-02-10 16:13:11 浏览: 99
json.zip_JSON_Json::Value_c++ Key Value_jsoncpp
要使用machina.io中的C++解析JSON格式的数据,可以使用rapidjson库。以下是一个简单的示例代码:
```cpp
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
#include <string>
using namespace rapidjson;
int main() {
std::string json = "{\"enumList\":[{\"key\":\"zoom\",\"value\":\"zoom\",\"alias\":\"zoom\"},{\"key\":\"tencent\",\"value\":\"tencent\",\"alias\":\"腾讯\"},{\"key\":\"huaweiCloud\",\"value\":\"huaweiCloud\",\"alias\":\"华为\"},{\"key\":\"null\",\"value\":\"null\",\"alias\":\"无\"},{\"key\":\"feishuPersonal\",\"value\":\"feishuPersonal\",\"alias\":\"飞书个人\"}]}";
// 解析JSON
Document doc;
doc.Parse(json.c_str());
// 访问和输出数据
const Value& enumList = doc["enumList"];
for (SizeType i = 0; i < enumList.Size(); i++) {
const Value& item = enumList[i];
std::cout << "key: " << item["key"].GetString() << ", "
<< "value: " << item["value"].GetString() << ", "
<< "alias: " << item["alias"].GetString() << std::endl;
}
return 0;
}
```
输出结果:
```
key: zoom, value: zoom, alias: zoom
key: tencent, value: tencent, alias: 腾讯
key: huaweiCloud, value: huaweiCloud, alias: 华为
key: null, value: null, alias: 无
key: feishuPersonal, value: feishuPersonal, alias: 飞书个人
```
可以看到,我们成功地解析了JSON数据,并访问了其中的内容。
阅读全文