添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
// add another json object QJsonObject jsonAnotherObject; jsonAnotherObject["subJsonObject"] = "Hello another object"; jsonRoot["anotherObject"] = jsonAnotherObject; saveJsonDataToFile(jsonRoot); bool QJsonTest::saveJsonDataToFile(QJsonObject const &jsonObj) QJsonDocument jsonDoc(jsonObj); QString jsonString = jsonDoc.toJson(QJsonDocument::Indented); QFile saveFile("json.txt"); if (!saveFile.open(QFile::WriteOnly)) return false; saveFile.write(jsonString.toLocal8Bit()); saveFile.close(); return true; void QJsonTest::parseJsonDataFromFile() QFile openFile("json.txt"); if (!openFile.exists()) return; openFile.open(QFile::ReadOnly | QFile::Text); // get all file data to a Text Stream and convert it to QByteArray QTextStream inStream(&openFile); QString jsonString = inStream.readAll(); // make QJsonDocument from QByteArray QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toLocal8Bit()); QJsonObject jsonRoot = jsonDoc.object(); // parse boolean value bool booleanData = jsonRoot["isBoolean"].toBool(); // parse string value QString stringData = jsonRoot["addString"].toString(); // parse double value double doubleData = jsonRoot["addDouble"].toDouble(); // parse int value int intData = jsonRoot["addInt"].toInt(); // parse jsonArray value QJsonArray jsonArrayData = jsonRoot["jsonArray"].toArray(); std::for_each(jsonArrayData.begin(), jsonArrayData.end(), [](QJsonValue const &valueItem) { const QString arrayItemString = valueItem.toString(); // parse json sub Object QJsonObject jsonSubObj = jsonRoot["anotherObject"].toObject(); QString subObjString = jsonSubObj["subJsonObject"].toString();

自己对照上面的写了个例子

#include <QCoreApplication>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>
#include <QDebug>
#include <QJsonValue>
void writeJson() {
    QJsonObject parent;
    QJsonObject obj;
    obj.insert("Name", "Ace");
    obj.insert("PassWord", "man");
    QJsonArray array;
    array.append("Sabo");
    array.append("Monkey D. Luffy");
    obj.insert("chats", array);
    QJsonObject obj1;
    obj1.insert("Name", "boooK");
    obj1.insert("PassWord", "123456");
    QJsonArray array1;
    array1.append("Sabo");
    array1.append("Monkey D. Luffy");
    obj1.insert("chats", array1);
    parent.insert("Ace", obj);
    parent.insert("boooK", obj1);
    QJsonDocument doc(parent);
    QByteArray json = doc.toJson();
    QFile file("E:\\Project\\Qt\\QJson\\data.json");
    file.open(QFile::WriteOnly);
    file.write(json);
    file.close();
void readJson() {
    QFile file("E:\\Project\\Qt\\QJson\\data.json");
    file.open(QFile::ReadOnly);
    QByteArray json = file.readAll();
    file.close();
    QJsonDocument doc = QJsonDocument::fromJson(json);
    QJsonObject obj = doc.object();
    QStringList keys = obj.keys();
    for(int i = 0; i < keys.size(); ++i) {
        QString key = keys.at(i);
        QJsonValue value = obj.value(key);
        QJsonObject obj1 = value.toObject();
        // 获得子项的key
        //QStringList ls = obj.keys();
        //for(int j=0; i<ls.size(); ++j){
        // QJsonValue subVal = obj1.value(ls.at(i));
        QJsonValue tmp = obj1.value("Name");
        QString name = tmp.toString();
        tmp = obj1.value("PassWord");
        QString password = tmp.toString();
        tmp = obj1.value("chats");
        QJsonArray array = tmp.toArray();
        QStringList list;
        for(int j = 0; j < array.size(); j++) {
            list.append(array[j].toString());
        qDebug() << "Name: " << name << " Password: " << password << endl;
        qDebug() << "chats: " << list;