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

    你可能感兴趣的文章
    NodeJS报错 Fatal error: ENOSPC: System limit for number of file watchers reached, watch ‘...path...‘
    查看>>
    Nodejs教程09:实现一个带接口请求的简单服务器
    查看>>
    nodejs服务端实现post请求
    查看>>
    nodejs框架,原理,组件,核心,跟npm和vue的关系
    查看>>
    Nodejs模块、自定义模块、CommonJs的概念和使用
    查看>>
    nodejs生成多层目录和生成文件的通用方法
    查看>>
    nodejs端口被占用原因及解决方案
    查看>>
    Nodejs简介以及Windows上安装Nodejs
    查看>>
    nodejs系列之express
    查看>>
    nodejs系列之Koa2
    查看>>
    Nodejs连接mysql
    查看>>
    nodejs连接mysql
    查看>>
    NodeJs连接Oracle数据库
    查看>>
    nodejs配置express服务器,运行自动打开浏览器
    查看>>
    Nodemon 深入解析与使用
    查看>>
    node不是内部命令时配置node环境变量
    查看>>
    node中fs模块之文件操作
    查看>>
    Node中同步与异步的方式读取文件
    查看>>
    Node中的Http模块和Url模块的使用
    查看>>
    Node中自启动工具supervisor的使用
    查看>>