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();
}
相关推荐
忘川w22 分钟前
《网络安全与防护》知识点复习
笔记·安全·web安全·网络安全
二进制人工智能32 分钟前
【OpenGL学习】(四)统一着色和插值着色
c++·opengl
zkinglin1 小时前
AORSA编译指南
笔记·其他·能源
追风赶月、1 小时前
【QT】控件一(QWidget、Button、Label)
开发语言·qt
红石程序员2 小时前
VSCode配置C++项目全攻略
开发语言·c++·visual studio
wu~9702 小时前
计算机网络-自顶向下—第一章概述重点复习笔记
笔记·计算机网络
liulilittle3 小时前
通过高级处理器硬件指令集AES-NI实现AES-256-CFB算法并通过OPENSSL加密验证算法正确性。
linux·服务器·c++·算法·安全·加密·openssl
十秒耿直拆包选手4 小时前
Qt:Qt桌面程序正常退出注意事项
c++·qt
姆路4 小时前
Qt背景平铺
开发语言·qt
李元豪4 小时前
【行云流水AI笔记】根据上面泳道图,请问如果加入强化学习,在哪些模块添加比较好,返回添加后的泳道图。
人工智能·笔记