当前位置:网站首页>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);
}
边栏推荐
- Personal website construction tutorial | local website environment construction | website production tutorial
- 【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
- Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
- Wechat applet - simple diet recommendation (4)
- Swift set pickerview to white on black background
- Interview: is bitmap pixel memory allocated in heap memory or native
- DDOS攻击原理,被ddos攻击的现象
- Usage differences between isempty and isblank
- 钉钉、企微、飞书学会赚钱了吗?
- Lepton 无损压缩原理及性能分析
猜你喜欢
Timed disappearance pop-up
Who is the "conscience" domestic brand?
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package can be used directly - next
【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
ByteDance Interviewer: how to calculate the memory size occupied by a picture
最全是一次I2C总结
非技术部门,如何参与 DevOps?
【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...
非技術部門,如何參與 DevOps?
随机推荐
Have the bosses ever encountered such problems in the implementation of flinksql by Flink CDC mongdb?
橫向滾動的RecycleView一屏顯示五個半,低於五個平均分布
【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
[tips] get the x-axis and y-axis values of cdfplot function in MATLAB
【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...
Energy momentum: how to achieve carbon neutralization in the power industry?
Interview: how does the list duplicate according to the attributes of the object?
Singleton mode encapsulates activity management class
Lepton 无损压缩原理及性能分析
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
天龙八部TLBB系列 - 单体技能群伤
Coffeescript Chinese character to pinyin code
@Jsonadapter annotation usage
学习笔记6--卫星定位技术(上)
AtCoder Beginner Contest 258「ABCDEFG」
【小技巧】獲取matlab中cdfplot函數的x軸,y軸的數值
vscode的快捷键
Detailed explanation of the use of staticlayout
驱动制造业产业升级新思路的领域知识网络,什么来头?
Write double click event