当前位置:网站首页>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::ReadOnly2.# 将数据转化为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);
}
边栏推荐
- Click the picture in the mobile browser and the picture will not pop up
- 驱动制造业产业升级新思路的领域知识网络,什么来头?
- 【小技巧】獲取matlab中cdfplot函數的x軸,y軸的數值
- ArcGIS Pro creating features
- Write double click event
- RMS TO EAP通过MQTT简单实现
- MySQL digital type learning notes
- How to get the STW (pause) time of GC (garbage collector)?
- Universal double button or single button pop-up
- Have the bosses ever encountered such problems in the implementation of flinksql by Flink CDC mongdb?
猜你喜欢

一个程序员的职业生涯到底该怎么规划?

Window下线程与线程同步总结

Constraintlayout officially provides rounded imagefilterview

How to get the STW (pause) time of GC (garbage collector)?

AtCoder Beginner Contest 254「E bfs」「F st表维护差分数组gcd」

ConstraintLayout的流式布局Flow

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

@Serializedname annotation use

学习笔记6--卫星定位技术(上)

> Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
随机推荐
Redis如何实现多可用区?
字节跳动面试官:一张图片占据的内存大小是如何计算
C#函数返回多个值方法
Glide conclusion
《通信软件开发与应用》课程结业报告
Interview: how does the list duplicate according to the attributes of the object?
一个程序员的职业生涯到底该怎么规划?
Jupiter notebook shortcut key
报错:Module not found: Error: Can‘t resolve ‘XXX‘ in ‘XXXX‘
面试:Bitmap像素内存分配在堆内存还是在native中
《微信小程序-基础篇》小程序中的事件与冒泡
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
Should the dependency given by the official website be Flink SQL connector MySQL CDC, with dependency added
ConstraintLayout的流式布局Flow
@Jsonadapter annotation usage
RMS to EAP is simply implemented through mqtt
双容水箱液位模糊PID控制系统设计与仿真(Matlab/Simulink)
How can PostgreSQL CDC set a separate incremental mode, debezium snapshot. mo
Write double click event
Zblogphp breadcrumb navigation code