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);

     
  • 相关阅读:
    SQL Server 基础知识/数据类型/数值类型
    javascript中slice(),splice(),split(),substring(),substr()使用方法
    Sublime text设置快捷键让编写的HTML文件在打指定浏览器预览
    常用开发环境配置和使用技巧
    JavaScript 模块化简析
    MySQL重置root用户密码的方法(转)
    SpringMVC 文件上传配置,多文件上传,使用的MultipartFile(转)
    Postman 安装及使用入门教程(转)
    HTTP状态码:400500 错误代码
    (转)Eclipse快捷键大全,导包快捷键:ctrl+Shift+/
  • 原文地址:https://www.cnblogs.com/judes/p/9171791.html
Copyright © 2011-2022 走看看