当前位置:网站首页>QT implements JSON parsing
QT implements JSON parsing
2022-07-05 10:24:00 【InfoQ】
Premise points
1. First get the address Read json file
QFile file(QString::fromStdString(json));
bool bOpen = file.open(QIODevice::ReadOnly);
if (bOpen == false)
{
return item;
}
QByteArray data = file.readAll();
file.close();
QIODevice::ReadOnly2.# Translate data into json Can be read
1. First
QJsonDocument doc = QJsonDocument::fromJson(data);// Reading and writing json file
if (!doc.isObject())
{
return item;
}
QJsonDocument::fromJson(data)2. Display the content corresponding to the keyword
QStringList keys = obj.keys();
for(int i=0;i<keys.size();i++)
{
qDebug() << "key" << i << " is:" << keys.at(i);
}
3.json Different ways of reading files , And his type
1. The first one is json Format
{
"optionA": "aaa",
"optionB": "bbbb",
"score": 3
}
QJsonObject obj = doc.object();// encapsulation json object
item.content = obj["content"].toString().toStdString();
item.optionA = obj["optionA"].toString().toStdString();
item.optionB = obj["optionB"].toString().toStdString();
item.score = obj["score"].toInt();//int type direct toint Don't convert to string
2. The second kind json Format
"questinList": [{
"optionA": "aaa",
"optionB": "bbbb",
}, {
"optionA": "aaa",
"optionB": "bbbb",
}]
QJsonArray questinlist = root["questinlist"].toArray();
for(int i = 0; i < questinlist.count(); i++)
{
QJsonObject obj = questinlist.at(i).toObject();
item.content = obj["content"].toString().toStdString();
item.optionA = obj["optionA"].toString().toStdString();
item.optionB = obj["optionB"].toString().toStdString();
data.questinList.push_back(item);
}
边栏推荐
- ByteDance Interviewer: how to calculate the memory size occupied by a picture
- Error: module not found: error: can't resolve 'xxx' in 'XXXX‘
- 《天天数学》连载58:二月二十七日
- [paper reading] kgat: knowledge graph attention network for recommendation
- Have the bosses ever encountered such problems in the implementation of flinksql by Flink CDC mongdb?
- 天龙八部TLBB系列 - 单体技能群伤
- Zblogphp breadcrumb navigation code
- Activity enter exit animation
- Window下线程与线程同步总结
- 自动化规范检查软件如何发展而来?
猜你喜欢

Implementation of smart home project

B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条...

Energy momentum: how to achieve carbon neutralization in the power industry?

【小技巧】獲取matlab中cdfplot函數的x軸,y軸的數值

IDEA新建sprintboot项目

Pagoda panel MySQL cannot be started

Usage differences between isempty and isblank

ByteDance Interviewer: how to calculate the memory size occupied by a picture

非技术部门,如何参与 DevOps?

> Could not create task ‘:app:MyTest.main()‘. > SourceSet with name ‘main‘ not found.问题修复
随机推荐
RMS to EAP is simply implemented through mqtt
字节跳动面试官:一张图片占据的内存大小是如何计算
[paper reading] ckan: collaborative knowledge aware autonomous network for adviser systems
Have the bosses ever encountered such problems in the implementation of flinksql by Flink CDC mongdb?
伪类元素--before和after
Timed disappearance pop-up
Swift saves an array of class objects with userdefaults and nssecurecoding
Matrix processing practice
【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...
[tips] get the x-axis and y-axis values of cdfplot function in MATLAB
[论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation
横向滚动的RecycleView一屏显示五个半,低于五个平均分布
《天天数学》连载58:二月二十七日
Glide advanced level
Constrained layout flow
非技術部門,如何參與 DevOps?
学习笔记6--卫星定位技术(上)
Singleton mode encapsulates activity management class
AtCoder Beginner Contest 254「E bfs」「F st表维护差分数组gcd」
The horizontally scrolling recycleview displays five and a half on one screen, lower than the average distribution of five