当前位置:网站首页>QT (36) -rapidjson parsing nested JSON
QT (36) -rapidjson parsing nested JSON
2022-06-25 00:59:00 【Cloudy summer】
The previous article wrote the simplest call . This article describes nested parsing .
value value : character string , Numbers , Array ,object, Nested arrays ,Bool
Generate json file :
// Generate a string of json Format string , And analyze
//value value : character string , Numbers , Array ,object, Nested arrays ,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. Generate a dom Elements Document
rapidjson::Document::AllocatorType &allocator = jsonDoc.GetAllocator();
jsonDoc.SetObject(); // Set up root dom by object type
jsonDoc.AddMember("name", "test", allocator); //2. Add child nodes String type
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. Add child nodes Array type
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. Add child nodes object nesting 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. Add child nodes Array nesting object
jsonDoc.AddMember("healthy", true, allocator); //5. Add child nodes add to bool Type values
// 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("----- Generated Json:\n%s", strJson.c_str());
std::string strPath ="C:\\JsonFile.txt";
FILE* myFile = fopen(strPath.c_str(), "w"); Write to file windows Platform to use wb
if (myFile)
{
fputs(buffer.GetString(), myFile);
fclose(myFile);
} analysis json Format :
rapidjson::Document newDoc;
std::string strPath ="C:\\JsonFile.txt";
FILE* myFile = fopen(strPath.c_str(), "r"); //windows Platform use rb
if (myFile)
{
char buffer[256];
FileReadStream inputStream(myFile,buffer,sizeof(buffer)); //1. Create an input stream
newDoc.ParseStream<0>(inputStream); //2. Convert read content to dom Elements
fclose(myFile); //3. Close file , Very important
}
if (newDoc.HasParseError())
{
printf("Json Parse error:%d\n", newDoc.GetParseError());
}
else
{
//1. analysis "name":"test"
if (newDoc.HasMember("name"))
{
printf("name:%s\n", newDoc["name"].GetString());
}
else
{}
//2. analysis "age":20
if (newDoc.HasMember("age"))
{
printf("age:%d\n", newDoc["age"].GetInt());
}
else
{}
//3. analysis array 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. analysis 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. analysis Array nesting object [{"name":"book1", "isbn":"123"},{"name":"book2","isbn":"456"}]
if (newDoc.HasMember("book"))
{
rapidjson::Value book;
book = newDoc["book"];
// First 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 {}
}
边栏推荐
- Paint rounded rectangle
- Input series
- Virtual machine - network configuration
- Scrollview height cannot fill full screen
- Android SQLite database
- 【微服务|Sentinel】簇点链路|微服务集群环境搭建
- 在企业级开发过程中我发现有位同事用select * from where 条件 for update
- VNC viewer remote connection raspberry pie without display
- 通过kubernetes可视化界面(rancher)安装kibana
- Working principle analysis of kubernetes architecture core components
猜你喜欢

Thermodynamic diagram display correlation matrix

移动安全工具-jar

Kubernetes 架构核心组件工作原理解析

【微服务|Sentinel】Sentinel快速入门|构建镜像|启动控制台

Single blind box removal, social blind box and friend blind box program source code

51单片机多机通信

ros(25):rqt_image_view报错Unable to load plugin for transport ‘compressed‘, error string

Use of JMeter

QT(35)-操作EXCEL-QXlsx-QAxObject

Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
随机推荐
Tiktok wallpaper applet v1.0.2 function, new arrival function
腾讯云国际云服务器网络访问丢包问题解决办法
In the process of enterprise development, I found that a colleague used the select * from where condition for update
Kibana installation via kubernetes visual interface (rancher)
[distributed system design profile (2)] kV raft
傳輸層 以字節為單比特的滑動窗口技術
Applet opening traffic master
Tiktok wallpaper applet, starlight wallpaper applet version 2.0, upgraded version
我想问一下兴业证券怎么开户?通过链接办理股票开户安全吗
Only positive integers can be entered in the text box
Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
Easy to wear - drop down radio
What is test development? Can you find a job at this stage?
C#和C 的CAN通信实验
Tiktok wallpaper applet source code
Android SQLite database
D does not require opapply() as a domain
adb shell getevent
Custom animation (simulated win10 loading animation)
大厂高频软件测试面试题和答案都帮你准备好啦,备战金九银十