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();
}
相关推荐
kfepiza18 分钟前
`accept_ra` 和 `autoconf` 和 `forwarding` 的关系 笔记250404
linux·网络·笔记·tcp/ip·智能路由器·ip·tcp
Once_day36 分钟前
Linux错误(6)X64向量指令访问地址未对齐引起SIGSEGV
linux·c++·sse·x64·sigsegv·xmm0
kfepiza1 小时前
Debian编译安装mysql8.0.41源码包 笔记250401
数据库·笔记·mysql·debian·database
JhonKI1 小时前
【从零实现Json-Rpc框架】- 项目实现 - 客户端注册主题整合 及 rpc流程示意
c++·qt·网络协议·rpc·json
__lost1 小时前
为什么new分配在堆上,函数变量在栈上+递归调用时栈内存的变化过程
c++·内存分配
序属秋秋秋1 小时前
算法基础_基础算法【位运算 + 离散化 + 区间合并】
c语言·c++·学习·算法·蓝桥杯
jyyyx的算法博客2 小时前
【再探图论】深入理解图论经典算法
c++·算法·图论
念_ovo2 小时前
【算法/c++】利用中序遍历和后序遍历建二叉树
数据结构·c++·算法
Vitalia2 小时前
⭐算法OJ⭐寻找最短超串【动态规划 + 状态压缩】(C++ 实现)Find the Shortest Superstring
开发语言·c++·算法·动态规划·动态压缩
"_rainbow_"2 小时前
Qt添加资源文件
开发语言·qt