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();
}
相关推荐
星恒随风9 分钟前
四天学完前端基础三件套(JavaScript篇)
开发语言·前端·javascript·笔记
羊群智妍20 分钟前
2026 免费GEO监测:AI搜索优化实用工具推荐
笔记
十五年专注C++开发21 分钟前
TypePerf:Windows 命令行性能计数器工具(CPU利用率、内存利用率、GPU利用率等)
c++·windows·typeperf
GoKu~24 分钟前
QT视图界面
qt
王老师青少年编程27 分钟前
csp信奥赛C++高频考点专项训练之字符串 --【字符串排序】:字符排序
c++·字符串·csp·高频考点·信奥赛·字符串排序·字符排序
杜子不疼.35 分钟前
【 C++ AI 大模型接入 SDK】 - 日志模块
开发语言·javascript·c++
3Tony1 小时前
解决VScode报错:preLaunchTask“C/C++: gcc.exe 生成活动文件“已终止,退出代码为 -1.
c++·ide·vscode
C+++Python1 小时前
C++ 泛型编程 极简示例代码
开发语言·c++
宵时待雨1 小时前
回溯算法专题2:二叉树中的深搜
开发语言·数据结构·c++·笔记·算法·深度优先
奋斗的小乌龟2 小时前
langchain4j笔记-08
java·spring boot·笔记