博客
关于我
QT读取JSON文件并解析
阅读量:610 次
发布时间:2019-03-13

本文共 1450 字,大约阅读时间需要 4 分钟。

(1)JSON文件(config.json)

{    "autor": "yudabo",    "staff":[        {"name":"于大博1"},        {"name":"于大博2"},        {"name":"于大博3"},        {"name":"于大博4"},        {"name":"于大博5"},        {"name":"于大博6"},        {"name":"于大博7"},        {"name":"于大博8"},        {"name":"于大博9"},        {"name":"于大博10"}    ],    "data": { "name": "yudabo" }}

(2)解析文件

QFile file("config.json");file.open(QIODevice: :ReadOnly | QIODevice: :Text);QString value = file.readAll();file.close();QJsonParseError parseJsonErr;QJsonDocument document = QJsonDocument: :fromJson(value.toUtf8(), &parseJsonErr);if (! (parseJsonErr.error == QJsonParseError: :NoError)) {    QMessageBox: :about(NULL, "提示", "配置文件错误!");    return;}QJsonObject jsonObject = document.object();/*    // autor字段    qDebug()<< "jsonObject[autor]==" << jsonObject["autor"].toString();*/// staff字段if (jsonObject.contains(QStringLiteral("staff"))) {    QJsonValue arrayValue = jsonObject.value(QStringLiteral("staff"));    if (arrayValue.isArray()) {        QJsonArray array = arrayValue.toArray();        for (int i = 0; i < array.size(); i++) {            QJsonValue nameArray = array.at(i);            QJsonObject key = nameArray.toObject();            qDebug() << "key[name]==" << key["name"].toString();        }    }}// data字段QJsonValue jsonDataValue = jsonObject.value(QStringLiteral("data"));QJsonObject jsonData = jsonDataValue.toObject();qDebug()<< "jsonData [name]==" << jsonData ["name"].toString();

 

转载地址:http://vbbaz.baihongyu.com/

你可能感兴趣的文章
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
mutiplemap 总结
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>