• Qt+JSON简单例子


    Qt+JSON简单例子

    Qt+JSON

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include "json.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        init();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    int MainWindow::init()
    {
        int arr[] = { 1, 2, 3 };
        double darr[] = { 4.2, 5.2 };
        bool barr[] = { true, false, true, false };
        QString value = "str";
        QJsonObject obj;
        obj.insert("Name", "Apple");
        obj.insert("Color", "Red");
        obj.insert("Weight", 0.2);
    
        JSON* json = new JSON();
        json->writeJson("bool", true);
        json->writeJson("int", 1);
        json->writeJson("double", 2.4);
        // value must be QString, implicit conversion turns string literal into bool
        json->writeJson("string", value);
        json->writeJson("str2bool", "str");
        json->writeJson("bool array", barr, 4);
        json->writeJson("int array", arr, 3);
        json->writeJson("double array", darr, 2);
        json->writeJson("object", obj);
        qDebug() << json->getJson();
    
        QString Passwdfile("hello.json");
        json->saveJson(Passwdfile);
    
        QString json_data = json->toString();
        ui->textEdit->setText(json_data);
    
        //原文链接:https://blog.csdn.net/Cappuccino_jay/article/details/125619033
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    json.h

    #ifndef JSON_H
    #define JSON_H
    
    #include 
    #include 
    #include 
    #include 
    
    class JSON {
    public:
        JSON();
    
        QJsonObject getJson();
        QJsonObject loadJson(const QString& filepath);
        void writeJson(const QString key, bool value);
        void writeJson(const QString key, int value);
        void writeJson(const QString key, double value);
        void writeJson(const QString key, QString value);
        void writeJson(const QString key, bool* array, int length);
        void writeJson(const QString key, int* array, int length);
        void writeJson(const QString key, double* array, int length);
        void writeJson(const QString key, QJsonObject object);
        bool saveJson(const QString& filepath);
        QString toString();
    
    private:
        QJsonObject json;
    };
    #endif // JSON_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    json.cpp

    #include 
    #include 
    #include 
    
    #include "json.h"
    
    JSON::JSON()
    {
    }
    
    QJsonObject JSON::getJson()
    {
        return json;
    }
    
    QJsonObject JSON::loadJson(const QString& filepath)
    {
        QFile loadFile(filepath);
    
        if (!loadFile.open(QIODevice::ReadOnly))
            qDebug() << "Unable to load JSON file";
    
        QByteArray allData = loadFile.readAll();
        loadFile.close();
    
        QJsonParseError json_error;
        QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
    
        if (json_error.error != QJsonParseError::NoError)
            qDebug() << "JSON error!";
    
        QJsonObject rootObj = jsonDoc.object();
        return rootObj;
    }
    
    // NOTE: implicit conversion turns string literal into bool
    void JSON::writeJson(const QString key, bool value)
    {
        json.insert(key, value);
    }
    
    void JSON::writeJson(const QString key, int value)
    {
        json.insert(key, value);
    }
    
    void JSON::writeJson(const QString key, double value)
    {
        json.insert(key, value);
    }
    
    // value only support QString
    void JSON::writeJson(const QString key, QString value)
    {
        json.insert(key, QString(value));
    }
    
    void JSON::writeJson(const QString key, bool* array, int length)
    {
        QJsonArray arr;
        for (int i = 0; i < length; i++)
            arr.append(array[i]);
        json.insert(key, arr);
    }
    
    void JSON::writeJson(const QString key, int* array, int length)
    {
        QJsonArray arr;
        for (int i = 0; i < length; i++)
            arr.append(array[i]);
        json.insert(key, arr);
    }
    
    void JSON::writeJson(const QString key, double* array, int length)
    {
        QJsonArray arr;
        for (int i = 0; i < length; i++)
            arr.append(array[i]);
        json.insert(key, arr);
    }
    
    void JSON::writeJson(const QString key, QJsonObject object)
    {
        json.insert(key, object);
    }
    
    bool JSON::saveJson(const QString& filepath)
    {
        QJsonDocument document;
        document.setObject(json);
        QFile file(filepath);
    
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            qDebug() << "Fail to save contents to JSON file";
            return false;
        }
    
        file.write(document.toJson());
    
        return true;
    }
    
    QString JSON::toString()
    {
        QJsonDocument document;
        document.setObject(json);
        QByteArray byteArray = document.toJson(QJsonDocument::Compact);
        QString str(byteArray);
        return str;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    hello.json

    {
        "bool": true,
        "bool array": [
            true,
            false,
            true,
            false
        ],
        "double": 2.4,
        "double array": [
            4.2,
            5.2
        ],
        "int": 1,
        "int array": [
            1,
            2,
            3
        ],
        "object": {
            "Color": "Red",
            "Name": "Apple",
            "Weight": 0.2
        },
        "str2bool": true,
        "string": "str"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在这里插入图片描述

    example2

    json.h

    #ifndef JSON_H
    #define JSON_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    struct Today
    {
        int ID;
        QString date;
        char name[32];
    };
    
    class Json
    {
    public:
        Json();
    public:
        int init();
        bool saveJson(const QString& filepath, QJsonObject &json);
    
    private:
        //顺便给结构体给一个变量
        Today today;
    };
    #endif // JSON_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    json.cpp

    #include "json.h"
    
    Json::Json()
    {
        today.date = "20190911";
        today.ID = 001;
        int len = strlen("zhangsan");
        memcpy(today.name, "zhangsan", len);
        today.name[len] = '\0';
    }
    
    int Json::init()
    {
        QJsonObject json;//构建json对象json
        json.insert("ID", today.ID);
        json.insert("date", today.date);
        json.insert("name", today.name);
    
        QJsonDocument document;
        document.setObject(json);
        QByteArray byte_array = document.toJson(QJsonDocument::Compact);
        QString json_str(byte_array);
    
        QString Passwdfile("hello3.json");
        saveJson(Passwdfile, json);
    
        //原文链接:https://blog.csdn.net/ljwoainia/article/details/100735303
        return 0;
    }
    
    bool Json::saveJson(const QString& filepath, QJsonObject &json)
    {
        QJsonDocument document;
        document.setObject(json);
        QFile file(filepath);
    
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            qDebug() << "Fail to save contents to JSON file";
            return false;
        }
    
        file.write(document.toJson());
        file.close();
    
        qDebug() << "finish to save contents to JSON file" << endl;
    
        return true;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    {
        "ID": 1,
        "date": "20190911",
        "name": "zhangsan"
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    参考

  • 相关阅读:
    ES中SQL查询详解
    华为云云耀云服务器L实例评测|部署个人音乐流媒体服务器 navidrome
    MySQL数据库基本操作和完整性约束类型详解
    Spring学习笔记(七)SpringMVC入门
    Python——分支语句
    【Python 千题 —— 基础篇】输出可以被5整除的数
    微信视频号的项目玩法,视频号好物分享,只要你会剪辑,就可以去操作
    2312. 卖木头块
    接口测试 —— requests 的基本了解
    点餐小程序实战教程08-购物车功能开发
  • 原文地址:https://blog.csdn.net/liuqingsongmsdn2014/article/details/134062847