qt-C++笔记之json文件内容操作完整例程

qt-C++笔记之json文件内容操作完整例程

code review!

文章目录

1.运行输出

json 复制代码
读取到的 JSON 对象: {
    "Array": [
        "Item1",
        "Item2"
    ],
    "Name": "Example",
    "Value": 42
}

修改后的 JSON 对象: {
    "Array": [
        "Item1",
        "Item2",
        "Item3"
    ],
    "Modified": true,
    "Name": "Modified Example",
    "Value": 42
}

2.运行后的test.json文件内容

json 复制代码
{
    "Array": [
        "Item1",
        "Item2",
        "Item3"
    ],
    "Modified": true,
    "Name": "Modified Example",
    "Value": 42
}

3.main.cpp

cpp 复制代码
#include <QCoreApplication>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QFile>
#include <QIODevice>
#include <QDebug>

// 函数:从文件读取 JSON 对象
QJsonObject readJsonFromFile(const QString &filePath) {
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly)) {
        qWarning() << "无法打开文件进行读取:" << filePath;
        return QJsonObject();
    }

    QByteArray jsonData = file.readAll();
    file.close();

    QJsonDocument doc = QJsonDocument::fromJson(jsonData);
    if (doc.isNull()) {
        qWarning() << "解析 JSON 失败。";
        return QJsonObject();
    }

    return doc.object();
}

// 函数:将 JSON 对象写入文件
void writeJsonToFile(const QString &filePath, const QJsonObject &jsonObject) {
    QJsonDocument doc(jsonObject);
    QFile file(filePath);
    if (!file.open(QIODevice::WriteOnly)) {
        qWarning() << "无法打开文件进行写入:" << filePath;
        return;
    }

    file.write(doc.toJson());
    file.close();
}

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    QString filePath = "test.json";

    // 创建并写入初始 JSON 对象
    QJsonObject initialObj;
    initialObj["Name"] = "Example";
    initialObj["Value"] = 42;

    QJsonArray array;
    array.append("Item1");
    array.append("Item2");
    initialObj["Array"] = array;

    // 将初始 JSON 对象写入文件
    writeJsonToFile(filePath, initialObj);

    // 从文件读取 JSON 对象
    QJsonObject readObj = readJsonFromFile(filePath);

    // 打印读取到的 JSON 对象
    qDebug().noquote() << "读取到的 JSON 对象:" << QJsonDocument(readObj).toJson(QJsonDocument::Indented);

    // 修改读取到的 JSON 对象
    readObj["Modified"] = true;
    readObj["Name"] = "Modified Example";
    QJsonArray newArray = readObj["Array"].toArray();
    newArray.append("Item3");
    readObj["Array"] = newArray;

    // 将修改后的 JSON 对象再次写入文件
    writeJsonToFile(filePath, readObj);

    // 再次从文件读取修改后的 JSON 对象
    QJsonObject modifiedReadObj = readJsonFromFile(filePath);

    // 打印修改后的 JSON 对象
    qDebug().noquote() << "修改后的 JSON 对象:" << QJsonDocument(modifiedReadObj).toJson(QJsonDocument::Indented);

    return a.exec();
}
相关推荐
Echo``几秒前
2:QT联合HALCON编程—图像显示放大缩小
开发语言·c++·图像处理·qt·算法
一点.点3 分钟前
李沐动手深度学习(pycharm中运行笔记)——04.数据操作
pytorch·笔记·python·深度学习·pycharm·动手深度学习
想睡hhh1 小时前
c++STL——stack、queue、priority_queue的模拟实现
开发语言·c++·stl
Sunlight_7771 小时前
第六章 QT基础:6、QT的Qt 时钟编程
开发语言·qt·命令模式
cloues break.1 小时前
C++初阶----模板初阶
java·开发语言·c++
wwww.wwww2 小时前
Qt软件开发-摄像头检测使用软件V1.1
开发语言·c++·qt
共享家95272 小时前
栈相关算法题解题思路与代码实现分享
c++·leetcode
Wendy_robot2 小时前
【前缀和计算和+哈希表查找次数】Leetcode 560. 和为 K 的子数组
c++·算法·leetcode
一只鱼^_2 小时前
第十六届蓝桥杯大赛软件赛省赛 C/C++ 大学B组 [京津冀]
c语言·数据结构·c++·算法·贪心算法·蓝桥杯·动态规划