QT中的Json文件创建与解析

一、思路

        QT中JSON格式的创建和解析涉及到QJsonObject、QJsonArray、QJsonDocument这三个,分别对应json对象、json数组以及编码转换,帮助文件查阅这三个类即可知道如何使用。

二、例子

#include <QCoreApplication>

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

void writeJson()
{
    QJsonObject rootObj;
    //1.插入name字段
    rootObj.insert("name", "China");

    //2.插入info字段
    QJsonObject infoObj;

    infoObj.insert("capital", "beijing");
    infoObj.insert("asian", true);
    infoObj.insert("founded", 1949);

    rootObj.insert("info", infoObj);

    //3.插入provinces字段
    QJsonArray provincesArray;

    QJsonObject provinceSdObj;
    provinceSdObj.insert("name", "shandong");
    provinceSdObj.insert("capital", "jinan");

    QJsonObject provinceZjObj;
    provinceZjObj.insert("name", "zhejiang");
    provinceZjObj.insert("capital", "hangzhou");

    provincesArray.append(provinceSdObj);
    provincesArray.append(provinceZjObj);

    rootObj.insert("provinces", provincesArray);

    //4.将rootObj转换为JSON字符串
    QJsonDocument doc(rootObj);
    QByteArray json = doc.toJson();

    //5.1打印输出
    qDebug() << QString(json).toUtf8().data();

    //5.2将JSON字符串写入到文件
    QFile file("C:\\Users\\EDY\\Desktop\\QTtest\\JSON\\china.json");
    file.open(QFile::WriteOnly);
    file.write(json);
    file.close();
}

void fromJson()
{
    //1.读取文件
    QFile file("C:\\Users\\EDY\\Desktop\\QTtest\\JSON\\china.json");
    file.open(QFile::ReadOnly);
    QByteArray json = file.readAll();
    file.close();

    QJsonDocument doc = QJsonDocument::fromJson(json);
    if(!doc.isObject())
    {
        qDebug() << "Not an object!";
        return;
    }

    //2.开始解析
    QJsonObject obj = doc.object();
    QStringList keys = obj.keys();
    for(int i=0;i<keys.size();i++)
    {
        //获取key-value
        QString key = keys[i];
        QJsonValue value = obj.value(key);

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

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

            qDebug() << "  " << "capital" << ":" << capital;
            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 provinceObj = provinceArray[i].toObject();

                QString name = provinceObj["name"].toString();
                QString capital = provinceObj["capital"].toString();

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



}

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

//    writeJson();

    fromJson();

    return a.exec();
}

上述例子包含json文件的构建与解析。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值