当前位置:网站首页>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);
}
边栏推荐
- Pseudo class elements -- before and after
- Pagoda panel MySQL cannot be started
- Events and bubbles in the applet of "wechat applet - Basics"
- Glide conclusion
- ConstraintLayout官方提供圆角ImageFilterView
- Interview: how does the list duplicate according to the attributes of the object?
- Learning II of workmanager
- Z-blog template installation and use tutorial
- CSDN always jumps to other positions when editing articles_ CSDN sends articles without moving the mouse
- 历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
猜你喜欢
What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?
RMS to EAP is simply implemented through mqtt
字节跳动面试官:一张图片占据的内存大小是如何计算
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
驱动制造业产业升级新思路的领域知识网络,什么来头?
苹果 5G 芯片研发失败?想要摆脱高通为时过早
Fluent generates icon prompt logo widget
pytorch输出tensor张量时有省略号的解决方案(将tensor完整输出)
如何判断线程池已经执行完所有任务了?
Implementation of smart home project
随机推荐
ConstraintLayout的流式布局Flow
Using directive in angualr2 to realize that the picture size changes with the window size
Idea create a new sprintboot project
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
ConstraintLayout官方提供圆角ImageFilterView
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条...
【观察】跨境电商“独立站”模式崛起,如何抓住下一个红利爆发时代?
How to judge that the thread pool has completed all tasks?
mongoDB副本集
StaticLayout的使用详解
WorkManager学习一
微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
Interview: is bitmap pixel memory allocated in heap memory or native
字节跳动面试官:一张图片占据的内存大小是如何计算
钉钉、企微、飞书学会赚钱了吗?
AtCoder Beginner Contest 258「ABCDEFG」
把欧拉的创新带向世界 SUSE 要做那个引路人
Swift saves an array of class objects with userdefaults and nssecurecoding
[论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package can be used directly - next