当前位置:网站首页>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);
}
边栏推荐
- 官网给的这个依赖是不是应该为flink-sql-connector-mysql-cdc啊,加了依赖调
- MySQL character type learning notes
- MySQL digital type learning notes
- B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条...
- Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上
- C#函数返回多个值方法
- 程序员搞开源,读什么书最合适?
- 一种用于干式脑电图的高密度256通道电极帽
- @Serializedname annotation use
- 硬核,你见过机器人玩“密室逃脱”吗?(附代码)
猜你喜欢
How to get the STW (pause) time of GC (garbage collector)?
The most complete is an I2C summary
程序员搞开源,读什么书最合适?
Window下线程与线程同步总结
把欧拉的创新带向世界 SUSE 要做那个引路人
Timed disappearance pop-up
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
A high density 256 channel electrode cap for dry EEG
Implementation of smart home project
Cut off 20% of Imagenet data volume, and the performance of the model will not decline! Meta Stanford et al. Proposed a new method, using knowledge distillation to slim down the data set
随机推荐
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
能源势动:电力行业的碳中和该如何实现?
Comparison of batch merge between Oracle and MySQL
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
请问大佬们 有遇到过flink cdc mongdb 执行flinksql 遇到这样的问题的么?
学习笔记6--卫星定位技术(上)
橫向滾動的RecycleView一屏顯示五個半,低於五個平均分布
Pagoda panel MySQL cannot be started
RMS TO EAP通过MQTT简单实现
双容水箱液位模糊PID控制系统设计与仿真(Matlab/Simulink)
天龙八部TLBB系列 - 关于技能冷却和攻击范围数量的问题
How do programmers live as they like?
Window下线程与线程同步总结
To bring Euler's innovation to the world, SUSE should be the guide
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
pytorch输出tensor张量时有省略号的解决方案(将tensor完整输出)
驱动制造业产业升级新思路的领域知识网络,什么来头?
Advanced opencv:bgr pixel intensity map
PHP solves the problems of cache avalanche, cache penetration and cache breakdown of redis
如何写出高质量的代码?