Qt5 write modified data Json

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/a844651990/article/details/90489487

Foreword

  Qt support for JSON provides an easy to use c ++ API to parse, modify and save JSON data. It also supports to save the data in binary format, which is a direct "mmap", and access is very fast.

Read Json data

Json file, 1.json

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

program:

#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();
}

Write Json data

And file contents as above.

#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();
}

Use 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"
        }
    ]
}

Modify the Json data

May need to be modified after json Sometimes we read out the data, and then rewrite it here in fact, the key point is file.seek (0) , written from scratch, or modify will continue to write down resulting in duplication. This case is suitable json modify existing data. If you need to add or reduce'd better empty the files to re-write it.

#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();
}

Guess you like

Origin blog.csdn.net/a844651990/article/details/90489487