当前位置:网站首页>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);
}
边栏推荐
- StaticLayout的使用详解
- SAP UI5 ObjectPageLayout 控件使用方法分享
- Atcoder beginer contest 254 "e BFS" f st table maintenance differential array GCD "
- Apple 5g chip research and development failure? It's too early to get rid of Qualcomm
- 一个程序员的职业生涯到底该怎么规划?
- Events and bubbles in the applet of "wechat applet - Basics"
- 字节跳动面试官:一张图片占据的内存大小是如何计算
- 非技术部门,如何参与 DevOps?
- The Alipay in place function can't be found, and the Alipay in place function is offline
- ArcGIS Pro creating features
猜你喜欢
伪类元素--before和after
Swift set pickerview to white on black background
学习笔记5--高精地图解决方案
StaticLayout的使用详解
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
自动化规范检查软件如何发展而来?
到底谁才是“良心”国产品牌?
程序员如何活成自己喜欢的模样?
> Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
【小技巧】獲取matlab中cdfplot函數的x軸,y軸的數值
随机推荐
In wechat applet, after jumping from one page to another, I found that the page scrolled synchronously after returning
Excerpt from "sword comes" (VII)
面试:List 如何根据对象的属性去重?
A high density 256 channel electrode cap for dry EEG
Today in history: the first e-book came out; The inventor of magnetic stripe card was born; The pioneer of handheld computer was born
Error: module not found: error: can't resolve 'xxx' in 'XXXX‘
@JsonAdapter注解使用
天龙八部TLBB系列 - 单体技能群伤
天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
Atcoder beginer contest 254 "e BFS" f st table maintenance differential array GCD "
Interview: is bitmap pixel memory allocated in heap memory or native
到底谁才是“良心”国产品牌?
【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...
WorkManager的学习二
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
Usage differences between isempty and isblank
微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
Customize the left sliding button in the line in the applet, which is similar to the QQ and Wx message interface
CSDN always jumps to other positions when editing articles_ CSDN sends articles without moving the mouse
把欧拉的创新带向世界 SUSE 要做那个引路人