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

    你可能感兴趣的文章
    PbootCMS entrance.php SQL注入漏洞复现
    查看>>
    PbootCMS 前台RCE漏洞复现
    查看>>
    PBT
    查看>>
    PB级分析型数据库ClickHouse的应用场景和特性
    查看>>
    pc3-12800
    查看>>
    PCA---主成成分分析
    查看>>
    PCA和自动编码器:每个人都能理解的算法
    查看>>
    pca算法
    查看>>
    PCA降维demo
    查看>>
    SharePoint 2013 图文开发系列之定义站点模板
    查看>>
    PCB生产流程详解-ChatGPT4o作答
    查看>>
    PCB设计十条黄金法则
    查看>>
    SpringSecurity框架介绍
    查看>>
    PCI Express学习篇:Power Management(二)
    查看>>
    pcie握手机制_【博文连载】PCIe扫盲——Ack/Nak 机制详解(一)
    查看>>
    pcm转wav的方法及代码示例
    查看>>
    PC史上最悲剧的16次失败
    查看>>
    PC端恶意代码分析Lab1.1-5.1,从零基础到精通,收藏这篇就够了!
    查看>>
    PC端稳定性测试探索
    查看>>
    PC端编辑 但能在PC端模拟移动端预览的富文本编辑器
    查看>>