Qt構建與解析Json示例

xhubobo發表於2024-11-11

本文以B站UP主“明王講QT”的【QT開發專題-天氣預報】中的章節內容作為學習Qt中構建、解析Json的參考方法。

1、Json文字

{
    "info": {
        "asian": true,
        "captical": "beijing",
        "founded": 1949
    },
    "name": "China",
    "provinces": [
        {
            "captical": "jinan",
            "name": "shandong"
        },
        {
            "captical": "hangzhou",
            "name": "zhejiang"
        }
    ]
}

2、構建、解析程式碼

#include <QtCore/QCoreApplication>
#include <QDir>
#include <QFile>

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>

void writeJson()
{
    QJsonObject rootObject;

    //1.插入name欄位
    rootObject.insert("name", "China");

    //2.插入info欄位
    QJsonObject infoObject;
    infoObject.insert("captical", "beijing");
    infoObject.insert("asian", true);
    infoObject.insert("founded", 1949);

    rootObject.insert("info", infoObject);

    //3.插入provinces欄位
    QJsonArray provinceArray;

    //山東
    QJsonObject provinceSdObject;
    provinceSdObject.insert("name", "shandong");
    provinceSdObject.insert("captical", "jinan");

    //浙江
    QJsonObject provinceZjObject;
    provinceZjObject.insert("name", "zhejiang");
    provinceZjObject.insert("captical", "hangzhou");

    provinceArray.append(provinceSdObject);
    provinceArray.append(provinceZjObject);

    rootObject.insert("provinces", provinceArray);

    //4.將rootObject轉換為字串
    QJsonDocument doc(rootObject);
    QByteArray json = doc.toJson();

    //列印輸出
    qDebug() << QString(json).toUtf8().data();

    //5.將json字串寫入檔案
    QString dirPath = QCoreApplication::applicationDirPath();
    QString path = QDir::cleanPath(dirPath + QDir::separator() + "china.json");
    QFile file(path);
    file.open(QFile::WriteOnly);
    file.write(json);
    file.close();
}

void readJson()
{
    //1.從檔案讀取json字串
    QString dirPath = QCoreApplication::applicationDirPath();
    QString path = QDir::cleanPath(dirPath + QDir::separator() + "china.json");
    QFile file(path);
    file.open(QFile::ReadOnly);
    QByteArray json = file.readAll();
    file.close();

    //2.解析json字串
    QJsonDocument doc = QJsonDocument::fromJson(json);
    if (!doc.isObject())
    {
        qDebug() << "Not an object";
        return;
    }

    QJsonObject rootObject = doc.object();
    QStringList keys = rootObject.keys();
    for (int i=0; i<keys.size(); i++)
    {
        QString key = keys[i];
        QJsonValue value = rootObject.value(key);

        if (value.isBool())
        {
            qDebug() << key << ":" << value.toBool();
        }
        else if (value.isString())
        {
            qDebug() << key << ":" << value.toString();
        }
        else if (value.isDouble())
        {
            qDebug() << key << ":" << value.toDouble();
        }
        else if (value.isObject())
        {
            qDebug() << key << ":";

            QJsonObject infoObject = value.toObject();
            QString captical = infoObject["captical"].toString();
            bool asian = infoObject["asian"].toBool();
            int founded = infoObject["founded"].toInt();

            qDebug() << "    captical: " << captical;
            qDebug() << "    asian: " << asian;
            qDebug() << "    founded: " << founded;
        }
        else if (value.isArray())
        {
            qDebug() << key << ":";

            QJsonArray provinceArray = value.toArray();
            for (int i=0; i<provinceArray.size(); i++)
            {
                QJsonObject provinceObject = provinceArray[i].toObject();
                QString name = provinceObject["name"].toString();
                QString captical = provinceObject["captical"].toString();

                qDebug() << "    name: " << name << ", captical: " << captical;
            }
        }
    }
}

相關文章