当前位置:网站首页>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::ReadOnly
2.# 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);
}
边栏推荐
- 面试:Bitmap像素内存分配在堆内存还是在native中
- What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?
- 伪类元素--before和after
- Pseudo class elements -- before and after
- Detailed explanation of the use of staticlayout
- SLAM 01.人类识别环境&路径的模型建立
- Atcoder beginer contest 254 "e BFS" f st table maintenance differential array GCD "
- 字节跳动面试官:一张图片占据的内存大小是如何计算
- 自动化规范检查软件如何发展而来?
- 天龙八部TLBB系列 - 单体技能群伤
猜你喜欢
To bring Euler's innovation to the world, SUSE should be the guide
【观察】跨境电商“独立站”模式崛起,如何抓住下一个红利爆发时代?
自动化规范检查软件如何发展而来?
> Could not create task ‘:app:MyTest.main()‘. > SourceSet with name ‘main‘ not found.问题修复
"Everyday Mathematics" serial 58: February 27
Learning Note 6 - satellite positioning technology (Part 1)
Timed disappearance pop-up
WorkManager的学习二
Redis如何实现多可用区?
ByteDance Interviewer: how to calculate the memory size occupied by a picture
随机推荐
@JsonAdapter注解使用
DDOS攻击原理,被ddos攻击的现象
QT VT100 parser
Detailed explanation of the use of staticlayout
Redis如何实现多可用区?
"Everyday Mathematics" serial 58: February 27
程序员如何活成自己喜欢的模样?
How does redis implement multiple zones?
Pseudo class elements -- before and after
面试:List 如何根据对象的属性去重?
How can PostgreSQL CDC set a separate incremental mode, debezium snapshot. mo
Atcoder beginer contest 254 "e BFS" f st table maintenance differential array GCD "
Using directive in angualr2 to realize that the picture size changes with the window size
WorkManager學習一
Comparison of batch merge between Oracle and MySQL
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
RMS to EAP is simply implemented through mqtt
leetcode:1200. 最小绝对差
[tips] get the x-axis and y-axis values of cdfplot function in MATLAB
Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)