当前位置:网站首页>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);
}
边栏推荐
- [paper reading] kgat: knowledge graph attention network for recommendation
- How to judge that the thread pool has completed all tasks?
- 【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
- How can PostgreSQL CDC set a separate incremental mode, debezium snapshot. mo
- 把欧拉的创新带向世界 SUSE 要做那个引路人
- 官网给的这个依赖是不是应该为flink-sql-connector-mysql-cdc啊,加了依赖调
- 天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
- In wechat applet, after jumping from one page to another, I found that the page scrolled synchronously after returning
- Timed disappearance pop-up
- Lepton 无损压缩原理及性能分析
猜你喜欢
随机推荐
面试:List 如何根据对象的属性去重?
Glide advanced level
Swift tableview style (I) system basic
Learning Note 6 - satellite positioning technology (Part 1)
The most complete is an I2C summary
[system design] index monitoring and alarm system
钉钉、企微、飞书学会赚钱了吗?
php解决redis的缓存雪崩,缓存穿透,缓存击穿的问题
Fluent generates icon prompt logo widget
StaticLayout的使用详解
各位大佬,我测试起了3条线程同时往3个mysql表中写入,每条线程分别写入100000条数据,用了f
AtCoder Beginner Contest 258「ABCDEFG」
How to plan the career of a programmer?
学习笔记6--卫星定位技术(上)
Flink CDC cannot monitor MySQL logs. Have you ever encountered this problem?
Learning notes 5 - high precision map solution
【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
Apple 5g chip research and development failure? It's too early to get rid of Qualcomm
驱动制造业产业升级新思路的领域知识网络,什么来头?
Constrained layout flow









