zoukankan      html  css  js  c++  java
  • Qt+json

    Json文件是这样:

    {
        "first fruit":
        {
            "describe":"an apple",
            "icon":"appleIcon",
            "name":"apple"
        },
        "second fruit":
        {
            "describe":"an orange",
            "icon":"orangeIcon",
            "name":"orange"
        },
        "three fruit array":
        [
            "eat 0",
            "eat 1",
            "eat 2",
            "eat 3",
            "eat 4"
        ]
    }

    存放在本地磁盘。 
    利用QJson可以方便的进行解析。

       QFile loadFile("D:\1.json");
    
        if(!loadFile.open(QIODevice::ReadOnly))
        {
            qDebug() << "could't open projects json";
            return;
        }
    
        QByteArray allData = loadFile.readAll();//第一步读取文件到BA
        loadFile.close();
    
        QJsonParseError json_error;
        QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));//第二步将BA内容转为json文档
    
        if(json_error.error != QJsonParseError::NoError)
        {
            qDebug() << "json error!";
            return;
        }
    
        QJsonObject rootObj = jsonDoc.object();//第三步获取json文档对象
    
        //输出所有key,这一步是非必须的,放最后的话,你可能读不到任何key
        QStringList keys = rootObj.keys();//第四步获取json文档对象的所有keys
        for(int i = 0; i < keys.size(); i++)
        {
            qDebug() << "key" << i << " is:" << keys.at(i);
        }
    
        //因为是预先定义好的JSON数据格式,所以这里可以这样读取
        if(rootObj.contains("first fruit"))//第五步根据keys获取内容
        {
           QJsonObject subObj = rootObj.take("first fruit").toObject();
           qDebug() << "describe is:" << subObj["describe"].toString();
           qDebug() << "icon is:" << subObj["icon"].toString();
           qDebug() << "name is:" << subObj["name"].toString();
        }
    
        if(rootObj.contains("second fruit"))
        {
           QJsonObject subObj = rootObj.take("second fruit").toObject();
           qDebug() << "describe is:" << subObj.take("describe").toString();
           qDebug() << "icon is:" << subObj.take("icon").toString();
           qDebug() << "name is:" << subObj.take("name").toString();
        }
    
        if(rootObj.contains("three fruit array"))
        {
           QJsonArray subArray = rootObj.take("three fruit array").toArray();
           for(int i = 0; i< subArray.size(); i++)
           {
               qDebug() << i<<" value is:" << subArray.at(i).toString();
           }
        }

    需要包含的头文件:

    #include <QJsonDocument>
    #include <QJsonParseError>
    #include <QFile>
    #include <QJsonObject>
    #include <QDebug>
    #include <QJsonArray>

    运行结果:

    key 0  is: "first fruit"
    key 1  is: "second fruit"
    key 2  is: "three fruit array"
    describe is: "an apple"
    icon is: "appleIcon"
    name is: "apple"
    describe is: "an orange"
    icon is: "orangeIcon"
    name is: "orange"
    0  value is: "eat 0"
    1  value is: "eat 1"
    2  value is: "eat 2"
    3  value is: "eat 3"
    4  value is: "eat 4"

    以上转:https://blog.csdn.net/hp_cpp/article/details/80338116


    PS:如何写json

    QJsonObject roadJsonSubObject;
    roadJsonSubObject.insert("IP", ui.lineEdit_5->text());
    roadJsonSubObject.insert("PORT", ui.lineEdit_7->text());

    QJsonDocument document;
    document.setObject(roadJsonSubObject);
    QByteArray ba = document.toJson(QJsonDocument::Compact);

     
  • 相关阅读:
    微信小程序开发之tab导航栏
    微信小程序开发之日期组件
    微信小程序运行机制
    iis部署错误:HTTP 错误 500.21
    web API请求与参数获取
    C#API接口调试工具
    微信小程序之自定义组件与使用
    微信小程序开发之拼接json数组字符串
    IdentityServer4 中文文档 -11- (快速入门)添加基于 OpenID Connect 的用户认证
    IdentityServer4 中文文档 -10- (快速入门)使用密码保护API
  • 原文地址:https://www.cnblogs.com/judes/p/9171791.html
Copyright © 2011-2022 走看看