Qt5读写修改Json数据

博客介绍了Qt对JSON数据的处理。Qt提供易于使用的C++ API解析、修改和保存JSON数据,还支持二进制格式保存,访问快。详细阐述了读、写JSON数据的操作,以及修改JSON数据的要点,如修改现有数据需从头写,添加或减少数据可清空重写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

  Qt中对JSON支持提供了一个易于使用的c++ API来解析、修改和保存JSON数据。它还支持以二进制格式保存这些数据,这种格式是直接“mmap”的,并且访问起来非常快。

读Json数据

Json文件,1.json

{
	"name": "flywm",
    "age": "18",
    "home": "tianjin" 
}

程序:

#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QDebug>

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

    QFile file("D:/1.json");
    if(!file.open(QIODevice::ReadOnly)) {
        qDebug() << "File open failed!";
    } else {
        qDebug() <<"File open successfully!";
    }

    QJsonDocument jdc(QJsonDocument::fromJson(file.readAll()));
    QJsonObject obj = jdc.object();
    qDebug() << obj.value("name").toString()
             << obj.value("age").toString()
             << obj.value("home").toString();
}

写Json数据

和上面的文件内容一样。

#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QDebug>

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

    QFile file("D:/1.json");
    if(!file.open(QIODevice::WriteOnly)) {
        qDebug() << "File open failed!";
    } else {
        qDebug() <<"File open successfully!";
    }

    QJsonObject obj;
    obj["age"] = "18";
    obj["name"] = "flywm";
    obj.insert("home", "tianjin");
    QJsonDocument jdoc(obj);
    file.write(jdoc.toJson());
    file.flush();
}

使用QJsonArray:

#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QDebug>

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

    QFile file("D:/1.json");
    if(!file.open(QIODevice::WriteOnly)) {
        qDebug() << "File open failed!";
    } else {
        qDebug() <<"File open successfully!";
    }

    QJsonObject rootObj;

    QJsonObject obj;
    obj["age"] = "18";
    obj["name"] = "flywm";
    obj.insert("home", "tianjin");

    QJsonObject obj1;
    obj1["age"] = "19";
    obj1["name"] = "flywm";
    obj1.insert("home", "tianjin");

    QJsonArray classArray;
    classArray.append(obj);
    classArray.append(obj1);

    rootObj["class"] = classArray;

    QJsonDocument jdoc(rootObj);
    file.write(jdoc.toJson());
    file.flush();
}

json:

{
    "class": [
        {
            "age": "18",
            "home": "tianjin",
            "name": "flywm"
        },
        {
            "age": "19",
            "home": "tianjin",
            "name": "flywm"
        }
    ]
}

修改Json数据

有时候我们读出来json数据之后可能会需要修改,这里其实就是再重写一遍,关键点是file.seek(0),从头开始写,不然修改的会继续往下写造成重复。这种情况适合修改现有json数据。如果需要添加或者减少最好还是将文件清空重新写入吧。

#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QDebug>

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

    QFile file("D:/1.json");
    if(!file.open(QIODevice::ReadWrite)) {
        qDebug() << "File open failed!";
    } else {
        qDebug() <<"File open successfully!";
    }

    QJsonDocument jdc(QJsonDocument::fromJson(file.readAll()));
    QJsonObject obj = jdc.object();
    // 修改 age
    obj["age"] = "99";
    jdc.setObject(obj);
    file.seek(0);
    file.write(jdc.toJson());
    file.flush();
}
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值