QT Json

QtJ Json 库

摘要: 在Qt中如何处理json数据

思维导图

QT Json
QT Json


#include <QCoreApplication>
#include <QJsonObject>
#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonParseError>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
//    QJsonObject json1{
//        {"city","上海"},
//        {"country","中国"}
//    };
//    json1.insert("name","lihua");
//    QJsonObject json2{
//        {"data","2012/01/01"},
//        {"week","星期一"}
//    };
//    QJsonArray aray;
//    aray.push_back(json1);
//    aray.push_back(json2);
//    qDebug()<<json1<<endl<<json2<<endl<<aray;
//    QJsonDocument jsondoc(aray);
//    QByteArray ba = jsondoc.toJson();
//    QFile file("result.json");
//    if(!file.open(QIODevice::WriteOnly)){
//        qDebug()<<"写入文件失败";
//        return 0;
//    }
//    file.write(ba);
//    file.close();
    QFile file("tianqi.json");
    if(!file.open(QIODevice::ReadOnly)){
        qDebug()<<"文件不能读";
        return 0;
    }
    QByteArray ba = file.readAll();
    qDebug()<<"读出的数据如下:";
//    qDebug()<<ba;
    QJsonParseError e;
    QJsonDocument jsondoc =
            QJsonDocument::fromJson(ba,&e);
    if(e.error == QJsonParseError::NoError && !jsondoc.isNull()){
        qDebug()<<jsondoc;
    }
    QJsonObject json;
    json = jsondoc.object();
    qDebug()<<json.size();
    qDebug()<<json.find("status")->toInt();
    qDebug()<<json.find("message")->toString();
    qDebug()<<json.find("result")->toObject();
    QJsonObject json1 = json.find("result")->toObject();
    qDebug()<<json1.find("location")->toObject();
    qDebug()<<json1.find("now")->toObject();
    QJsonArray array1 = json1.find("forecasts")->toArray();
    QStringList list;
    list<<"text_day"<<"text_night"<<"high"<<"low";
    for(QJsonValue v:array1){
        if(v.isObject()){
            QJsonObject json = v.toObject();
            for(QJsonValue v:json){
                if(v.isString()){
                    qDebug()<<v.toString();
                }else if(v.isDouble()){
                    qDebug()<<v.toDouble();
                }else if(v.isUndefined()){
                    continue;
                }
            }
        }
    }
    return a.exec();
}


//result.json 文件容
{
    "status": 0,
    "result": {
        "location": {
            "country": "中国",
            "province": "吉林省",
            "city": "延边朝鲜族自治州",
            "name": "龙井",
            "id": "222405"
        },
        "now": {
            "text": "阴",
            "temp": 24,
            "feels_like": 24,
            "rh": 65,
            "wind_class": "1级",
            "wind_dir": "东风",
            "uptime": "20200630160500"
        },
        "forecasts": [
            {
                "text_day": "阵雨",
                "text_night": "多云",
                "high": 22,
                "low": 14,
                "wc_day": "<3级",
                "wd_day": "东风",
                "wc_night": "<3级",
                "wd_night": "东风",
                "date": "2020-06-30",
                "week": "星期二"
            },
            {
                "text_day": "多云",
                "text_night": "多云",
                "high": 26,
                "low": 15,
                "wc_day": "<3级",
                "wd_day": "东风",
                "wc_night": "<3级",
                "wd_night": "东风",
                "date": "2020-07-01",
                "week": "星期三"
            },
            {
                "text_day": "多云",
                "text_night": "多云",
                "high": 28,
                "low": 16,
                "wc_day": "<3级",
                "wd_day": "东风",
                "wc_night": "<3级",
                "wd_night": "东风",
                "date": "2020-07-02",
                "week": "星期四"
            },
            {
                "text_day": "小雨",
                "text_night": "晴",
                "high": 28,
                "low": 17,
                "wc_day": "<3级",
                "wd_day": "东南风",
                "wc_night": "<3级",
                "wd_night": "东南风",
                "date": "2020-07-03",
                "week": "星期五"
            },
            {
                "text_day": "小雨",
                "text_night": "晴",
                "high": 27,
                "low": 17,
                "wc_day": "<3级",
                "wd_day": "东南风",
                "wc_night": "<3级",
                "wd_night": "东南风",
                "date": "2020-07-04",
                "week": "星期六"
            }
        ]
    },
    "message": "success"
}

QT Json

上一篇:十位大牛做出的web前端开发规范总结


下一篇:mysql启动报错