当前位置:网站首页>Qt实现json解析
Qt实现json解析
2022-07-05 10:01:00 【InfoQ】
前提要点
1. 首先获取地址 读取json文件
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.# 将数据转化为json可读
1. 首先
QJsonDocument doc = QJsonDocument::fromJson(data);//读写json文档
if (!doc.isObject())
{
return item;
}
QJsonDocument::fromJson(data)
2.显示关键字所对应的内容
QStringList keys = obj.keys();
for(int i=0;i<keys.size();i++)
{
qDebug() << "key" << i << " is:" << keys.at(i);
}
3.json文件读取的不同方式,以及他的类型
1.第一种json格式
{
"optionA": "aaa",
"optionB": "bbbb",
"score": 3
}
QJsonObject obj = doc.object();//封装json对象
item.content = obj["content"].toString().toStdString();
item.optionA = obj["optionA"].toString().toStdString();
item.optionB = obj["optionB"].toString().toStdString();
item.score = obj["score"].toInt();//int 类型 直接toint 不用转为字符串
2.第二种json格式
"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);
}
边栏推荐
- 【小技巧】獲取matlab中cdfplot函數的x軸,y軸的數值
- Constrained layout flow
- Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)
- [tips] get the x-axis and y-axis values of cdfplot function in MATLAB
- @SerializedName注解使用
- Implementation of smart home project
- Wechat applet - simple diet recommendation (4)
- Coffeescript Chinese character to pinyin code
- 最全是一次I2C总结
- 苹果 5G 芯片研发失败?想要摆脱高通为时过早
猜你喜欢
WorkManager学习一
Redis如何实现多可用区?
@SerializedName注解使用
WorkManager的学习二
Meitu lost 300 million yuan in currency speculation for half a year. Huawei was exposed to expand its enrollment in Russia. Alphago's peers have made another breakthrough in chess. Today, more big new
Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes
Learning II of workmanager
如何獲取GC(垃圾回收器)的STW(暫停)時間?
驱动制造业产业升级新思路的领域知识网络,什么来头?
Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
随机推荐
Interview: how does the list duplicate according to the attributes of the object?
Write double click event
To bring Euler's innovation to the world, SUSE should be the guide
天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
橫向滾動的RecycleView一屏顯示五個半,低於五個平均分布
Should the dependency given by the official website be Flink SQL connector MySQL CDC, with dependency added
Comparison of batch merge between Oracle and MySQL
. Net delay queue
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条...
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
uniapp + uniCloud+unipay 实现微信小程序支付功能
横向滚动的RecycleView一屏显示五个半,低于五个平均分布
一种用于干式脑电图的高密度256通道电极帽
How do programmers live as they like?
StaticLayout的使用详解
Universal double button or single button pop-up
Advanced opencv:bgr pixel intensity map
Window下线程与线程同步总结
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
最全是一次I2C总结