Qt笔记-Qt中Json存二进制文件并读取文件(QJsonObject、QJsonArray、QJsonDocument的使用)

程序运行截图如下:

文件如下:

源码如下:

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

class Student{
public:
    int id;
    QString name;
    void write(QJsonObject &json) const{

        json["id"] = id;
        json["name"] = name;
    }

    void read(const QJsonObject &json){

        if (json.contains("id") && json["id"].isDouble())
            id = json["id"].toInt();

        if (json.contains("name") && json["name"].isString())
            name = json["name"].toString();
    }

    friend QDebug operator << (QDebug os, Student stu){

            os << "(" << stu.id << ", "
               << stu.name << ")";

            return os;
        }

};

class MyClass{
public:
    int id;
    QVector<Student> students;

    void read(const QJsonObject &json){

        if(json.contains("id") && json["id"].isDouble())
            id = json["id"].toInt();

        if(json.contains("students") && json["students"].isArray()){

            QJsonArray array = json["students"].toArray();
            for(int i = 0; i < array.size(); i++){

                QJsonObject obj = array[i].toObject();
                Student student;
                student.read(obj);
                this->students.append(student);
            }
        }

    }

    void write(QJsonObject &json) const{

        json["id"] = id;
        QJsonArray array;
        for(int i = 0; i < students.size(); i++){

            QJsonObject student;
            students.at(i).write(student);
            array.append(student);
        }
        json["students"] = array;
    }

    friend QDebug operator << (QDebug os, MyClass myClass){

            os << "(" << endl << myClass.id << ", ";
            for(int i = 0; i < myClass.students.size(); i++){

                os << myClass.students.at(i);
            }
            os << endl << ")";

            return os;
        }
};


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

    Student s0;
    s0.id = 1000;
    s0.name = "朱小明";

    Student s1;
    s1.id = 1001;
    s1.name = "闰土";

    Student s2;
    s2.id = 1000;
    s2.name = "锅盖";

    Student s3;
    s3.id = 1003;
    s3.name = "王炸";

    MyClass myClass;
    myClass.id = 142203;
    myClass.students.append(s0);
    myClass.students.append(s1);
    myClass.students.append(s2);
    myClass.students.append(s3);

    QJsonObject json;
    myClass.write(json);

    QJsonDocument saveJson(json);

    QFile saveJsonFile("save.json");
    QFile saveBinaryFile("save.dat");
    if(!saveJsonFile.open(QIODevice::ReadWrite)){

        qDebug() << "open failed";
        return 0;
    }

    if(!saveBinaryFile.open(QIODevice::ReadWrite)){

        qDebug() << "open failed";
        return 0;
    }

    saveJsonFile.write(saveJson.toJson());
    saveJsonFile.close();

    saveBinaryFile.write(saveJson.toBinaryData());
    saveBinaryFile.close();

    qDebug() << "write over";

    QFile loadFile("save.dat");
    if(!loadFile.open(QIODevice::ReadOnly)){

        qDebug() << "open failed";
    }

    QByteArray data = loadFile.readAll();
    QJsonDocument loadDoc(QJsonDocument::fromBinaryData(data));
    MyClass readClass;
    readClass.read(loadDoc.object());
    loadFile.close();

    qDebug() << "read over";
    qDebug() << readClass;

    return a.exec();
}

 

 

 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页