• JSON文件读写


    1、依赖文件

    #include 
    #include 
    #include 
    #include 
    #include 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、头文件

    bool ReadJsonFile(const QString& filePath="");
    bool WriteJsonFile(const QString& filePath="");
    
    • 1
    • 2

    3、源文件

    bool Widget::ReadJsonFile(const QString& filePath/*=""*/)
    {
        // 读取JSON文件
        QFile file("data.json");
        if (!file.open(QIODevice::ReadOnly)) 
        {
            qDebug() << "Failed to open JSON file.";
            return false;
        }
        QByteArray jsonData = file.readAll();
        file.close();
        
        // 解析JSON数据
        QJsonParseError parseError;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &parseError);
        if (parseError.error != QJsonParseError::NoError) 
        {
            qDebug() << "Failed to parse JSON:" << parseError.errorString();
            return false;
        }
        
        // 获取根对象
        QJsonObject jsonObj = jsonDoc.object();
        
        // 读取JSON数据
        QString name = jsonObj["name"].toString();
        int age = jsonObj["age"].toInt();
        
        qDebug() << "name:" << name;
        qDebug() << "age:" << age;
        
        QJsonObject jsonSubObj = jsonObj["hobbies"].toObject();
        QString type1 = jsonSubObj["type1"].toString();
        QString type2 = jsonSubObj["type2"].toString();
        
        qDebug() << "type1:" << type1;
        qDebug() << "type2:" << type2;
    
        return true;
    }
    bool Widget::WriteJsonFile(const QString& filePath/*=""*/)
    {
        // 创建 JSON 对象
        QJsonObject jsonObj;
        jsonObj["name"] = "JSON";
        jsonObj["age"] = 20;
        
        QJsonObject jsonSubObj;
        jsonSubObj["type1"] = "Reading";
        jsonSubObj["type2"] = "Music";
        jsonObj["hobbies"] = jsonSubObj;
        
        // 将 JSON 对象转换为 JSON 文档
        QJsonDocument jsonDoc(jsonObj);
        
        // 将 JSON 文档转换为 JSON 字符串
        QByteArray jsonData = jsonDoc.toJson();
        
        // 写入 JSON 字符串到文件
        QFile file("data.json");
        if (file.open(QIODevice::WriteOnly | QIODevice::Text)) 
        {
            file.write(jsonData);
            file.close();
            qDebug() << "JSON data written to file successfully.";
        } 
        else
        {
            qDebug() << "Failed to open file for writing.";
        }
        
        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
    • 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

    4、执行结果

    name: "JSON"
    age: 20
    type1: "Reading"
    type2: "Music"
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    基于stm32单片机BMP180气压计海拔高度温度测量Proteus仿真
    网卡介绍篇
    【大数据采集工具-gobblin】
    cmd进程简单操作指令
    力扣labuladong——一刷day42
    redis之如何实现消息队列
    vite-plugin-vue-setup-extend和unplugin-auto-import让v3开发更丝滑
    <C++>快速掌握set 容器|去重的底层原因|使用仿函数定义排序规则
    将输入对象转换为数组数组的维度大于等于1numpy.atleast_1d()
    push,pop指令
  • 原文地址:https://blog.csdn.net/weixin_45053845/article/details/133591872