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

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

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"  }}

解析步骤

  • 读取文件
  • QFile file("config.json");file.setReadOnly(true);file.setText(true);file.open();QString value = file.readAll();file.close();QJsonParseError parseJsonErr;QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr);if (parseJsonErr.error != QJsonParseError::NoError) {    QMessageBox::about(NULL, "提示", "配置文件错误!");    return;}
    1. 解析JSON数据
    2. QJsonObject jsonObject = document.object();// 解析autor字段QDebug << "jsonObject[\"autor\"]==" << jsonObject["autor"].toString();// 解析staff字段if (jsonObject.contains("staff")) {    QJsonValue arrayValue = jsonObject.value("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 dataValue = jsonObject.value("data");QJsonObject data = dataValue.toObject();QDebug << "data[name]:" << data["name"].toString();

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

    你可能感兴趣的文章
    openEuler 正式开放:推动计算多样化时代的到来
    查看>>
    OpenEuler23.03欧拉系统_安装瀚高数据库企业版6.0.4_openeuler切换root用户_su:拒绝权限_passwd: 鉴定令牌操作错误---国产瀚高数据库工作笔记001
    查看>>
    OpenEuler23.03欧拉系统_安装瀚高数据库企业版6.0.4_踩坑_安装以后系统无法联网_启动ens33网卡---国产瀚高数据库工作笔记002
    查看>>
    OpenFeign 入门与实战
    查看>>
    OpenFeign源码学习
    查看>>
    OpenFeign组件声明式服务调用
    查看>>
    openfeign远程调用不起作用解决_使用Spring Boot的spring.factories进行注入---SpringCloud Alibaba_若依微服务框架改造---工作笔记007
    查看>>
    openfire开发(四)消息拦截器
    查看>>
    openfire源码解读之将cache和session对象移入redis以提升性能
    查看>>
    Openfire身份认证绕过漏洞复现+利用(CVE-2023-32315)
    查看>>
    OpenForest 开源项目安装与使用指南
    查看>>
    OpenGL glBlendFunc() 设置颜色混合 透明度叠加计算
    查看>>
    opengl 教程(15) 摄像机控制(2)
    查看>>
    opengl 深度详解,多重采样时,如何在OpenGL纹理中解析深度值?
    查看>>
    OpenGL 的内置矩阵种种
    查看>>
    OpenGL/OpenGL ES 入门:基础变换 - 初识向量/矩阵
    查看>>
    OpenGL中shader读取实现
    查看>>
    OpenGL中旋转平移缩放等变换的顺序对模型的影响
    查看>>
    Opengl中的gluProject函数认识
    查看>>
    OpenGl介绍
    查看>>