博客
关于我
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/

    你可能感兴趣的文章
    Netty工作笔记0011---Channel应用案例2
    查看>>
    Netty工作笔记0014---Buffer类型化和只读
    查看>>
    Netty工作笔记0050---Netty核心模块1
    查看>>
    Netty工作笔记0084---通过自定义协议解决粘包拆包问题2
    查看>>
    Netty常见组件二
    查看>>
    netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
    查看>>
    Netty核心模块组件
    查看>>
    Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
    查看>>
    Netty源码—2.Reactor线程模型一
    查看>>
    Netty源码—4.客户端接入流程一
    查看>>
    Netty源码—4.客户端接入流程二
    查看>>
    Netty源码—5.Pipeline和Handler一
    查看>>
    Netty源码—6.ByteBuf原理二
    查看>>
    Netty源码—7.ByteBuf原理三
    查看>>
    Netty源码—7.ByteBuf原理四
    查看>>
    Netty源码—8.编解码原理二
    查看>>
    Netty源码解读
    查看>>
    Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
    查看>>
    Netty相关
    查看>>
    Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
    查看>>