当前位置:网站首页>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 {}
}
边栏推荐
- [distributed system design profile (2)] kV raft
- 108页(4万字)未来公寓智能化设计平台项目方案建议书2022版
- Sanic服务启动失败
- EVM简略
- 移动安全工具-apktool
- Jar package merging using Apache ant
- Activity startup process
- Custom animation (simulated win10 loading animation)
- Apk decompiled method (not confused)
- Custom animation (simulated win10 loading animation) - Optimization
猜你喜欢

108页(4万字)未来公寓智能化设计平台项目方案建议书2022版

Single blind box removal, social blind box and friend blind box program source code
Is it so difficult to calculate the REM size of the web page according to the design draft?

生成订单30分钟未支付,则自动取消,该怎么实现?

VNC viewer remote connection raspberry pie without display
@mysql

无需显示屏的VNC Viewer远程连接树莓派

Binder mechanism and Aidl communication example

【Redis实现秒杀业务②】超卖问题的解决方案

Tiktok wallpaper applet, starlight wallpaper applet version 2.0, upgraded version
随机推荐
Apk decompiled method (not confused)
2022r1 quick opening pressure vessel operation test questions and answers
大厂高频软件测试面试题和答案都帮你准备好啦,备战金九银十
Use of JMeter
December 6, 2019 what happens after the browser enters a URL
Xcode预览(Preview)显示List视图内容的一个Bug及解决
QT(36)-rapidjson解析嵌套的json
Alternative to log4j
Helm chart仓库操作
Databinding quick start (still using findviewbyid?)
A small crawler program written by beginners
Tiktok wallpaper applet v1.0.2 function, new arrival function
2021-09-12
More pictures | explain the Nacos parameters in detail!
Binder mechanism and Aidl communication example
[distributed system design profile (2)] kV raft
Simple collation of Web cache
Practical operation notes - notebook plus memory and ash cleaning
adb shell sendevent
How to reduce the font size of custom controls (optimize the round dot progress bar)