当前位置:网站首页>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);
}
边栏推荐
- AtCoder Beginner Contest 258「ABCDEFG」
- Apple 5g chip research and development failure? It's too early to get rid of Qualcomm
- Personal website construction tutorial | local website environment construction | website production tutorial
- Tianlong Babu TLBB series - questions about skill cooling and the number of attack ranges
- Glide conclusion
- flink cdc不能监听mysql日志,大家遇到过这个问题吧?
- SAP UI5 ObjectPageLayout 控件使用方法分享
- [paper reading] kgat: knowledge graph attention network for recommendation
- Activity jump encapsulation
- ConstraintLayout的流式布局Flow
猜你喜欢

Learning Note 6 - satellite positioning technology (Part 1)

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

Constrained layout flow

伪类元素--before和after

How did automated specification inspection software develop?
![[paper reading] kgat: knowledge graph attention network for recommendation](/img/fa/d2061bc7bd437f062d46a009cf32cf.png)
[paper reading] kgat: knowledge graph attention network for recommendation

【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...

uniapp + uniCloud+unipay 实现微信小程序支付功能

@Serializedname annotation use

学习笔记5--高精地图解决方案
随机推荐
Matrix processing practice
Dedecms website building tutorial
A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo
To bring Euler's innovation to the world, SUSE should be the guide
LiveData 面试题库、解答---LiveData 面试 7 连问~
What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?
Singleton mode encapsulates activity management class
Interview: how does the list duplicate according to the attributes of the object?
Window下线程与线程同步总结
《通信软件开发与应用》课程结业报告
苹果 5G 芯片研发失败?想要摆脱高通为时过早
Write double click event
Click the picture in the mobile browser and the picture will not pop up
C语言实现QQ聊天室小项目 [完整源码]
C function returns multiple value methods
Zblogphp breadcrumb navigation code
Comparison of batch merge between Oracle and MySQL
Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)
Who is the "conscience" domestic brand?
Flink CDC cannot monitor MySQL logs. Have you ever encountered this problem?