使用JsonCpp库 解析以下字符串{"success":true,"code":"0","message":"Success","data":{"codeList":"","list":[{"id":"1514610634240335880","code":"blood_cell","name":"血球","checkItemOne":null},{"id":"1514610634240335881","code":"biochemical","name":"生化","checkItemOne":null}]},"timestamp":1689488017878}
时间: 2023-07-17 11:12:12 浏览: 49
以下是使用JsonCpp库解析给定JSON字符串的示例代码:
```cpp
#include <iostream>
#include <json/json.h>
int main() {
std::string jsonString = "{\"success\":true,\"code\":\"0\",\"message\":\"Success\",\"data\":{\"codeList\":\"\",\"list\":[{\"id\":\"1514610634240335880\",\"code\":\"blood_cell\",\"name\":\"血球\",\"checkItemOne\":null},{\"id\":\"1514610634240335881\",\"code\":\"biochemical\",\"name\":\"生化\",\"checkItemOne\":null}]},\"timestamp\":1689488017878}";
// 解析JSON字符串
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse(jsonString, root);
if (!parsingSuccessful) {
std::cout << "Failed to parse JSON" << std::endl;
return 1;
}
// 访问解析后的JSON数据
bool success = root["success"].asBool();
std::string code = root["code"].asString();
std::string message = root["message"].asString();
long long timestamp = root["timestamp"].asInt64();
Json::Value dataList = root["data"]["list"];
for (int i = 0; i < dataList.size(); i++) {
std::string id = dataList[i]["id"].asString();
std::string code = dataList[i]["code"].asString();
std::string name = dataList[i]["name"].asString();
std::cout << "ID: " << id << ", Code: " << code << ", Name: " << name << std::endl;
}
return 0;
}
```
在上述示例中,我们通过Json::Reader对象的parse()函数将给定的JSON字符串解析为Json::Value对象。然后,我们可以使用Json::Value对象来访问和操作解析后的JSON数据。
在这个示例中,我们访问了"success"、"code"、"message"和"timestamp"字段,并将它们转换为对应的数据类型。然后,我们遍历了"data.list"字段的数组,并访问了数组中每个元素的"id"、"code"和"name"字段。
运行以上代码,你会看到输出结果为:
```
ID: 1514610634240335880, Code: blood_cell, Name: 血球
ID: 1514610634240335881, Code: biochemical, Name: 生化
```
希望这个示例能够帮助你理解如何使用JsonCpp库解析给定的JSON字符串。如果有任何进一步的问题,请随时提问。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)