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();
}
相关推荐
Juan_20123 分钟前
P1041题解
c++·算法·题解·搜索
冷崖12 分钟前
const 与 constexpr
c++·学习
wenjie学长20 分钟前
[UE学习笔记]—划时代意义的两大功能—lumen和Nanite
笔记·学习·ue·三维数字化
摇滚侠29 分钟前
Spring Boot 3零基础教程,WEB 开发 国际化 Spring Boot + Thymeleaf 笔记45
spring boot·笔记·后端
枫叶丹440 分钟前
【Qt开发】多元素类控件(三)-> QTreeWidget
开发语言·数据库·c++·qt
hansang_IR1 小时前
【题解】P2217 [HAOI2007] 分割矩阵 [记忆化搜索]
c++·数学·算法·记忆化搜索·深搜
degen_2 小时前
第二次进入PEICORE流程
c语言·笔记
Voyager_43 小时前
算法学习记录03——二叉树学习笔记:从两道题看透后序位置的关键作用
笔记·学习·算法
云泽8084 小时前
函数模板与类模板:C++泛型编程核心解析
java·开发语言·c++
我先去打把游戏先5 小时前
ESP32学习笔记(基于IDF):ESP32连接MQTT服务器
服务器·笔记·单片机·嵌入式硬件·学习·esp32