当前位置:网站首页>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);
}
边栏推荐
- IDEA新建sprintboot项目
- 【JS】数组降维
- Matrix processing practice
- 微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
- [C language] the use of dynamic memory development "malloc"
- The king of pirated Dall · e? 50000 images per day, crowded hugging face server, and openai ordered to change its name
- Activity jump encapsulation
- 请问postgresql cdc 怎么设置单独的增量模式呀,debezium.snapshot.mo
- RMS to EAP is simply implemented through mqtt
- RMS TO EAP通过MQTT简单实现
猜你喜欢

Timed disappearance pop-up

最全是一次I2C总结

MySQL character type learning notes

苹果 5G 芯片研发失败?想要摆脱高通为时过早
![[论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation](/img/fa/d2061bc7bd437f062d46a009cf32cf.png)
[论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation

ByteDance Interviewer: how to calculate the memory size occupied by a picture

Workmanager Learning one

程序员搞开源,读什么书最合适?

Wechat applet - simple diet recommendation (4)

双容水箱液位模糊PID控制系统设计与仿真(Matlab/Simulink)
随机推荐
Wechat applet - simple diet recommendation (4)
《剑来》语句摘录(七)
Flink CDC cannot monitor MySQL logs. Have you ever encountered this problem?
How to plan the career of a programmer?
Energy momentum: how to achieve carbon neutralization in the power industry?
How does redis implement multiple zones?
Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
@JsonAdapter注解使用
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
Workmanager Learning one
双容水箱液位模糊PID控制系统设计与仿真(Matlab/Simulink)
程序员搞开源,读什么书最合适?
How can PostgreSQL CDC set a separate incremental mode, debezium snapshot. mo
请问postgresql cdc 怎么设置单独的增量模式呀,debezium.snapshot.mo
Workmanager learning 1
报错:Module not found: Error: Can‘t resolve ‘XXX‘ in ‘XXXX‘
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
Pagoda panel MySQL cannot be started
【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
Kotlin compose multiple item scrolling