当前位置:网站首页>QT(36)-rapidjson解析嵌套的json
QT(36)-rapidjson解析嵌套的json
2022-06-24 20:06:00 【多云的夏天】
前面一篇写了最简单的调用。本篇描述的是嵌套解析。
value值:字符串,数字,数组,object,嵌套数组,Bool
生成json文件:
//生成一串如下的json格式字符串,并解析
//value值:字符串,数字,数组,object,嵌套数组,Bool
// {
// "name":"test",
// "age":20,
// "letter":["a","b","c"],
// "location": {"province":"fujian","city":"xiamen","number":16}
// "book":[{"name":"book1", "isbn":"123"},{"name":"book2","isbn":"456"}],
// "healthy":true,
// }
rapidjson::Document jsonDoc; //1.生成一个dom元素Document
rapidjson::Document::AllocatorType &allocator = jsonDoc.GetAllocator();
jsonDoc.SetObject(); // 设置root dom为object型
jsonDoc.AddMember("name", "test", allocator); //2.添加子节点 字符串型
jsonDoc.AddMember("age", 20, allocator);
rapidjson::Value letterArray(rapidjson::kArrayType);
letterArray.PushBack("a", allocator);
letterArray.PushBack("b", allocator);
letterArray.PushBack("c", allocator);
jsonDoc.AddMember("letter", letterArray, allocator); //3.添加子节点 数组型
rapidjson::Value locationObj(rapidjson::kObjectType);
locationObj.AddMember("province", "fujian", allocator);
locationObj.AddMember("city", "xiamen", allocator);
locationObj.AddMember("number", 16, allocator);
jsonDoc.AddMember("location", locationObj, allocator); //4.添加子节点 object 嵌套object
rapidjson::Value bookArray(rapidjson::kArrayType);
rapidjson::Value book1(rapidjson::kObjectType);
book1.AddMember("name", "book1", allocator);
book1.AddMember("isbn", "123", allocator);
bookArray.PushBack(book1, allocator);
rapidjson::Value book2(rapidjson::kObjectType);
book2.AddMember("name", "book2", allocator);
book2.AddMember("isbn", "456", allocator);
bookArray.PushBack(book2, allocator);
jsonDoc.AddMember("book", bookArray, allocator); //5.添加子节点 数组嵌套object
jsonDoc.AddMember("healthy", true, allocator); //5.添加子节点 添加bool类型值
// jsonDoc.AddMember("sports", NULL, allocator);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
jsonDoc.Accept(writer);
std::string strJson = std::string( buffer.GetString() );
printf("-----生成的Json:\n%s", strJson.c_str());
std::string strPath ="C:\\JsonFile.txt";
FILE* myFile = fopen(strPath.c_str(), "w"); 写到文件windows平台要使用wb
if (myFile)
{
fputs(buffer.GetString(), myFile);
fclose(myFile);
}解析json格式:
rapidjson::Document newDoc;
std::string strPath ="C:\\JsonFile.txt";
FILE* myFile = fopen(strPath.c_str(), "r"); //windows平台使用rb
if (myFile)
{
char buffer[256];
FileReadStream inputStream(myFile,buffer,sizeof(buffer)); //1.创建一个输入流
newDoc.ParseStream<0>(inputStream); //2.将读取的内容转换为dom元素
fclose(myFile); //3.关闭文件,很重要
}
if (newDoc.HasParseError())
{
printf("Json Parse error:%d\n", newDoc.GetParseError());
}
else
{
//1.解析 "name":"test"
if (newDoc.HasMember("name"))
{
printf("name:%s\n", newDoc["name"].GetString());
}
else
{}
//2.解析 "age":20
if (newDoc.HasMember("age"))
{
printf("age:%d\n", newDoc["age"].GetInt());
}
else
{}
//3.解析 array 数组 "letter":["a","b","c"],
if (newDoc.HasMember("letter"))
{
rapidjson::Value letter;
letter = newDoc["letter"];
if (letter.IsArray() && !letter.Empty())
{
for (rapidjson::SizeType i = 0; i < letter.Size(); i++)
{
printf("letter:%s\n", letter[i].GetString());
}
}
else
{}
}
else {}
//3.解析 object {"province":"fujian","city":"xiamen","number":16}
if (newDoc.HasMember("location"))
{
rapidjson::Value location;
location = newDoc["location"];
if (location.IsObject())
{
if (location.HasMember("province"))
{
printf("location:province:%s\n", location["province"].GetString());
}
else {}
if (location.HasMember("city"))
{
printf("location:city:%s\n", location["city"].GetString());
}
else {}
if (location.HasMember("number"))
{
printf("location:number:%d\n", location["number"].GetInt());
}
else {}
}
else {}
}
else {}
//4.解析 数组嵌套object [{"name":"book1", "isbn":"123"},{"name":"book2","isbn":"456"}]
if (newDoc.HasMember("book"))
{
rapidjson::Value book;
book = newDoc["book"];
//先取Array
if (book.IsArray() && !book.Empty())
{
rapidjson::Value tempBook;
for (rapidjson::SizeType i = 0; i < book.Size(); i++)
{
tempBook = book[i];
if (tempBook.IsObject())
{
if (tempBook.HasMember("name") && tempBook.HasMember("isbn"))
{
printf("book:%d:name:%s, isbn:%s\n", i, tempBook["name"].GetString(), tempBook["isbn"].GetString());
}
else {}
}
else {}
}
}
else {}
}
else {}
if (newDoc.HasMember("healthy"))
{
if (newDoc["healthy"].GetBool())
{
printf("healthy:true\n");
}
else
{
printf("healthy:false\n");
}
}
else {}
}
边栏推荐
- Kibana installation via kubernetes visual interface (rancher)
- A small crawler program written by beginners
- Uniapp encapsulated incentive advertisement, screen insert advertisement and banner advertisement
- vim使用命令
- ros(25):rqt_image_view报错Unable to load plugin for transport ‘compressed‘, error string
- Interesting checkbox counters
- 2021-11-07
- Use of JMeter easynmon
- Use and click of multitypeadapter in recycleview
- December 6, 2019 what happens after the browser enters a URL
猜你喜欢

【微服务|Sentinel】实时监控|RT|吞吐量|并发数|QPS

Use of navigation and navigationui

Creative SVG ring clock JS effect

Svg+js keyboard control path

How to quickly open traffic master for wechat applet

Tiktok wallpaper applet source code

What is the difference between one way and two way ANOVA analysis, and how to use SPSS or prism for statistical analysis

Interesting checkbox counters

【Redis实现秒杀业务④】一人一单,不可重复购买

无需显示屏的VNC Viewer远程连接树莓派
随机推荐
Use and click of multitypeadapter in recycleview
Using tcp/udp tools to debug the yeelight ribbon
Common redis commands in Linux system
C# Winform 最大化遮挡任务栏和全屏显示问题
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL
Use coordinatorlayout+appbarlayout+collapsingtoolbarlayout to create a collapsed status bar
【Redis实现秒杀业务④】一人一单,不可重复购买
Wx applet jump page
打卡smart精灵#1,品牌不缺吸引力,产品本身实力如何?
JS dynamically generates variable names and assigns values
传输层 以字节为单位的滑动窗口技术
UE4 WebBrowser chart cannot display problems
Simple collation of Web cache
【Redis实现秒杀业务②】超卖问题的解决方案
A plug-in framework for implementing registration free and login verification with hook technology
Svg+js keyboard control path
热力图展示相关矩阵
2021-02-15
通过kubernetes可视化界面(rancher)安装kibana
adb shell sendevent