当前位置:网站首页>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);
}
边栏推荐
- 橫向滾動的RecycleView一屏顯示五個半,低於五個平均分布
- How to plan the career of a programmer?
- How to judge that the thread pool has completed all tasks?
- How to write high-quality code?
- Livedata interview question bank and answers -- 7 consecutive questions in livedata interview~
- Timed disappearance pop-up
- Events and bubbles in the applet of "wechat applet - Basics"
- Universal double button or single button pop-up
- Glide conclusion
- Activity enter exit animation
猜你喜欢
![[paper reading] ckan: collaborative knowledge aware autonomous network for adviser systems](/img/6c/5b14f47503033bc2c85a259a968d94.png)
[paper reading] ckan: collaborative knowledge aware autonomous network for adviser systems

学习笔记5--高精地图解决方案

Workmanager learning 1

《天天数学》连载58:二月二十七日

A high density 256 channel electrode cap for dry EEG

Learning note 4 -- Key Technologies of high-precision map (Part 2)

AtCoder Beginner Contest 258「ABCDEFG」

【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB

Universal double button or single button pop-up

Detailed explanation of the use of staticlayout
随机推荐
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
Click the picture in the mobile browser and the picture will not pop up
IDEA新建sprintboot项目
报错:Module not found: Error: Can‘t resolve ‘XXX‘ in ‘XXXX‘
How did automated specification inspection software develop?
Idea create a new sprintboot project
vscode的快捷键
苹果 5G 芯片研发失败?想要摆脱高通为时过早
Atcoder beginer contest 254 "e BFS" f st table maintenance differential array GCD "
微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
SLAM 01.人类识别环境&路径的模型建立
Learning II of workmanager
Implementation of smart home project
Fluent generates icon prompt logo widget
Usage differences between isempty and isblank
Activity enter exit animation
Singleton mode encapsulates activity management class
Using directive in angualr2 to realize that the picture size changes with the window size
Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
How to write high-quality code?