Qt平台下使用QJson解析JSON字符串

2020-07-16 19:55:38 浏览数 (1)

前言

上一篇介绍了C语言写的JSON解析库cJSON的使用:使用cJSON库解析和构建JSON字符串

本篇文章介绍,Qt开发环境下QJson库的使用示例。示例代码包含简单和复杂JSON字符串的解析和构建,Qt工程已经开源在Github和Gitee代码托管平台。

  • Github仓库地址:

https://github.com/whik/QJson_Demo

  • Gitee仓库地址:

https://gitee.com/whik/QJson_Demo

开发平台基于Qt 5.8 Windows。示例的JSON字符串和上一篇使用的是一样的。

JSON解析示例包括:

  • 和风天气实时数据
  • 心知天气实时数据
  • 心知天气3天预报数据
  • 城市空气质量AQI信息
  • 全国油价信息
  • 北京时间等。

由于文章篇幅限制,本篇文章先介绍QJson的解析示例。

QJson解析JSON示例

JSON的解析要对照JSON字符串来理解,关于JSON字符串的介绍,可以参考JSON简介

解析和构建都要先包含如下头文件:

代码语言:javascript复制
#include <QJsonDocument>#include <QJsonObject>#include <QJsonArray>
示例字符串1:和风天气实时数据

这个JSON字符串中HeWeather6键的值是一个数组,数组内只有1个JSON对象,这个对象里又嵌套了几个JSON对象。

代码语言:javascript复制
{    "HeWeather6": [{        "basic": {            "cid": "CN101010700",            "location": "昌平",            "parent_city": "北京",            "admin_area": "北京",            "cnty": "中国",            "lat": "40.21808624",            "lon": "116.23590851",            "tz": " 8.00"        },        "update": {            "loc": "2019-07-20 10:21",            "utc": "2019-07-20 02:21"        },        "status": "ok",        "now": {            "cloud": "96",            "cond_code": "104",            "cond_txt": "阴",            "fl": "28",            "hum": "86",            "pcpn": "0.0",            "pres": "995",            "tmp": "25",            "vis": "4",            "wind_deg": "100",            "wind_dir": "东风",            "wind_sc": "1",            "wind_spd": "4"        }    }]}

字符串1解析函数

主要是JSON的多层嵌套的解析。

代码语言:javascript复制
int Parse_HeWeather_Now_Json(void){    QJsonParseError err_rpt;    QJsonDocument  root_Doc = QJsonDocument::fromJson(he_now_json, &err_rpt);//字符串格式化为JSON
    if(err_rpt.error != QJsonParseError::NoError)    {        qDebug() << "JSON格式错误";        return -1;    }    else    //JSON格式正确    {        //        qDebug() << "JSON格式正确:n" << root_Doc;
        QJsonObject root_Obj = root_Doc.object();        QJsonValue weather_Value = root_Obj.value("HeWeather6");    //HeWeather6键的值,是一个数组        if(weather_Value.isArray()) //可省略        {            QJsonObject weather_Obj = weather_Value.toArray().at(0).toObject();   //HeWeather6数组就含有一个元素0
            /* basic键信息 */            QJsonObject basic_Obj = weather_Obj.value("basic").toObject();            QString cid = basic_Obj.value("cid").toString();            QString location = basic_Obj.value("location").toString();            QString parent_city = basic_Obj.value("parent_city").toString();            QString cnty = basic_Obj.value("cnty").toString();            QString lat = basic_Obj.value("lat").toString();            QString lon = basic_Obj.value("lon").toString();            QString basic_info = cid   " "   parent_city   " "   cnty   " "   lat   " "   lon;            qDebug() << basic_info;
            /* update键信息 */            QJsonObject update_Obj = weather_Obj.value("update").toObject();            QString loc = "当地时间:"   update_Obj.value("loc").toString();   //当地时间            QString utc = "UTC时间:"   update_Obj.value("utc").toString();   //UTC时间            QString status = "解析状态:"   weather_Obj.value("status").toString();    //"ok"            qDebug() << loc   " "   utc   " "   status;
            /* now键信息*/            QJsonObject now_Obj = weather_Obj.value("now").toObject();            QString cond_txt = "白天天气:"   now_Obj.value("cond_txt").toString();            QString hum = "湿度:"   now_Obj.value("hum").toString();            QString tmp = "温度:"   now_Obj.value("tmp").toString();            QString wind_dir = "风向:"    now_Obj.value("wind_dir").toString();            QString wind_sc = "风级:"   now_Obj.value("wind_sc").toString();            qDebug() << cond_txt   " "   hum   " "   tmp   " "   wind_dir   " "   wind_sc;        }        qDebug() << "解析完成!";    }    return 0;}
示例字符串2:心知天气实时数据

这个字符串和上面那个一样,都是数组元素是JSON对象,对象的值又是一个JSON对象。

代码语言:javascript复制
{    "results": [{        "location": {            "id": "WX4FBXXFKE4F",            "name": "北京",            "country": "CN",            "path": "北京,北京,中国",            "timezone": "Asia/Shanghai",            "timezone_offset": " 08:00"        },        "now": {            "text": "晴",            "code": "1",            "temperature": "-7"        },        "last_update": "2018-12-06T22:05:00 08:00"    }]}

字符串2解析函数

代码语言:javascript复制
int Parse_Seniverse_Now_Json(void){    QJsonParseError err_rpt;    QJsonDocument  root_Doc = QJsonDocument::fromJson(seniverse_now_json, &err_rpt);//字符串格式化为JSON    if(err_rpt.error != QJsonParseError::NoError)    {        qDebug() << "JSON格式错误";        return -1;    }    else    //JSON格式正确    {        //        qDebug() << "JSON格式正确:n" << root_Doc;        QJsonObject root_Obj = root_Doc.object();        QJsonValue result_Value = root_Obj.value("results");        if(result_Value.isArray())        {            QJsonObject result_Obj = result_Value.toArray().at(0).toObject();
            QString last_update = result_Obj.value("last_update").toString();            qDebug() << last_update;
            /* location键的值 */            QJsonObject location_Obj = result_Obj.value("location").toObject();            QString id = location_Obj.value("id").toString();            QString name = location_Obj.value("name").toString();            QString timezone = location_Obj.value("timezone").toString();            QString path = location_Obj.value("path").toString();            QString loc_str = id   " "   name   " "   timezone   " "   path ;            qDebug() << loc_str;
            /* now键 */            QJsonObject now_Obj = result_Obj.value("now").toObject();            QString code = "天气代码: "   now_Obj.value("code").toString();            QString temperature = "当前温度:"   now_Obj.value("temperature").toString();            QString text = "天气:"   now_Obj.value("text").toString();            qDebug() << code << temperature << text;        }    }    return 0;}
示例字符串3:心知3天天气预报数据
代码语言:javascript复制
{    "results": [{        "location": {            "id": "WS10730EM8EV",            "name": "深圳",            "country": "CN",            "path": "深圳,深圳,广东,中国",            "timezone": "Asia/Shanghai",            "timezone_offset": " 08:00"        },        "daily": [{            "date": "2018-12-06",            "text_day": "阴",            "code_day": "9",            "text_night": "阴",            "code_night": "9",            "high": "25",            "low": "16",            "precip": "",            "wind_direction": "无持续风向",            "wind_direction_degree": "",            "wind_speed": "10",            "wind_scale": "2"        }, {            "date": "2018-12-07",            "text_day": "阴",            "code_day": "9",            "text_night": "小雨",            "code_night": "13",            "high": "20",            "low": "15",            "precip": "",            "wind_direction": "北",            "wind_direction_degree": "0",            "wind_speed": "15",            "wind_scale": "3"        }, {            "date": "2018-12-08",            "text_day": "小雨",            "code_day": "13",            "text_night": "小雨",            "code_night": "13",            "high": "17",            "low": "12",            "precip": "",            "wind_direction": "东北",            "wind_direction_degree": "45",            "wind_speed": "15",            "wind_scale": "3"        }],        "last_update": "2018-12-06T18:00:00 08:00"    }]}

字符串3解析函数

代码语言:javascript复制
int Parse_Seniverse_Forecast_Json(void){    QJsonParseError err_rpt;    QJsonDocument  root_Doc = QJsonDocument::fromJson(seniverse_forcast_json, &err_rpt);//字符串格式化为JSON    if(err_rpt.error != QJsonParseError::NoError)    {        qDebug() << "JSON格式错误";        return -1;    }    else    //JSON格式正确    {        //        qDebug() << "JSON格式正确:n" << root_Doc;        QJsonObject root_Obj = root_Doc.object();        QJsonValue result_Value = root_Obj.value("results");        if(result_Value.isArray())        {            QJsonObject result_Obj = result_Value.toArray().at(0).toObject();
            QString last_update = result_Obj.value("last_update").toString();            qDebug() << last_update;            /* location键的值 */            QJsonObject location_Obj = result_Obj.value("location").toObject();            QString id = location_Obj.value("id").toString();            QString name = location_Obj.value("name").toString();            QString timezone = location_Obj.value("timezone").toString();            QString path = location_Obj.value("path").toString();            QString loc_str = id   " "   name   " "   timezone   " "   path ;            qDebug() << loc_str;
            /* daily预报天气3天,数组元素3个*/            QJsonValue daily_Vaule = result_Obj.value("daily");            if(daily_Vaule.isArray())            {                for(int idx = 0; idx <= 2; idx  )                {                    QJsonObject daily_Obj = daily_Vaule.toArray().at(idx).toObject();                    QString date = " 日期:"   daily_Obj.value("date").toString();                    QString text_day =" 白天天气:"    daily_Obj.value("text_day").toString();                    QString high = " 最高温度:"   daily_Obj.value("high").toString();                    QString low = " 最低温度:"   daily_Obj.value("low").toString();                    QString wind_direction = " 风向:"   daily_Obj.value("wind_direction").toString();                    QString wind_scale = " 风级:"   daily_Obj.value("wind_scale").toString();                    qDebug() << date   text_day   high   low   wind_direction   wind_scale;                }            }        }    }    return 0;}
示例字符串4:空气AQI质量指数

包含10个元素的数组。

代码语言:javascript复制
{    "status": "0",    "citye": "changchun",    "city": "长春",    "citycode": "101060101",    "aqi": "50",    "data": [{        "add": "长春",        "aqi": "50",        "pm25": "22",        "per": "优",        "lv": "1"    }, {        "add": "食品厂",        "aqi": "54",        "pm25": "18",        "per": "良",        "lv": "2"    }, {        "add": "客车厂",        "aqi": "52",        "pm25": "20",        "per": "良",        "lv": "2"    }, {        "add": "邮电学院",        "aqi": "35",        "pm25": "24",        "per": "优",        "lv": "1"    }, {        "add": "劳动公园",        "aqi": "45",        "pm25": "19",        "per": "优",        "lv": "1"    }, {        "add": "园林处",        "aqi": "45",        "pm25": "21",        "per": "优",        "lv": "1"    }, {        "add": "净月潭",        "aqi": "46",        "pm25": "30",        "per": "优",        "lv": "1"    }, {        "add": "甩湾子",        "aqi": "51",        "pm25": "24",        "per": "良",        "lv": "2"    }, {        "add": "经开区环卫处",        "aqi": "48",        "pm25": "25",        "per": "优",        "lv": "1"    }, {        "add": "高新区管委会",        "aqi": "51",        "pm25": "16",        "per": "良",        "lv": "2"    }, {        "add": "岱山公园",        "aqi": "49",        "pm25": "19",        "per": "优",        "lv": "1"    }]}

字符串4解析函数

代码语言:javascript复制
int Parse_AQI_Json(void){    QJsonParseError err_rpt;    QJsonDocument  root_Doc = QJsonDocument::fromJson(AQI_json, &err_rpt);//字符串格式化为JSON    if(err_rpt.error != QJsonParseError::NoError)    {        qDebug() << "JSON格式错误";        return -1;    }    else    //JSON格式正确    {        //        qDebug() << "JSON格式正确:n" << root_Doc;        QJsonObject root_Obj = root_Doc.object();
        QString city = root_Obj.value("city").toString();        QString citycode = root_Obj.value("citycode").toString();        QString citye = root_Obj.value("citye").toString();        QString status = root_Obj.value("status").toString();        qDebug() << city   " "   citycode   " "   citye   " "   status;
        /* data键 */        QJsonValue data_Vaule = root_Obj.value("data");        if(data_Vaule.isArray())        {            for(int idx = 0; idx <= 10; idx  )            {                QJsonObject data_Obj = data_Vaule.toArray().at(idx).toObject();                QString add = "地址:"   data_Obj.value("add").toString();                QString aqi = " AQI:"   data_Obj.value("aqi").toString();                QString lv = " 空气质量等级:"   data_Obj.value("lv").toString();                QString per = " 空气质量:"   data_Obj.value("per").toString();                QString pm25 = " PM2.5等级:"   data_Obj.value("pm25").toString();                qDebug() << add   aqi   lv   per   pm25;            }        }    }    return 0;}
示例字符串5:北京标准时间

比较简单一个JSON对象

代码语言:javascript复制
{    "success": "1",    "result": {        "timestamp": "1542456793",        "datetime_1": "2018-11-17 20:13:13",        "datetime_2": "2018年11月17日 20时13分13秒",        "week_1": "6",        "week_2": "星期六",        "week_3": "周六",        "week_4": "Saturday"    }}

字符串5解析函数

代码语言:javascript复制
int Parse_BJTime_Json(void){    QJsonParseError err_rpt;    QJsonDocument  root_Doc = QJsonDocument::fromJson(bj_time_json, &err_rpt);//字符串格式化为JSON    if(err_rpt.error != QJsonParseError::NoError)    {        qDebug() << "JSON格式错误";        return -1;    }    else    //JSON格式正确    {        //        qDebug() << "JSON格式正确:n" << root_Doc;        QJsonObject root_Obj = root_Doc.object();
        QString success = root_Obj.value("success").toString();        /* result键 */        QJsonObject result_Obj = root_Obj.value("result").toObject();        QString datetime_1 = result_Obj.value("datetime_1").toString();        QString datetime_2 = result_Obj.value("datetime_2").toString();        QString timestamp = result_Obj.value("timestamp").toString();        QString week_1 = result_Obj.value("week_1").toString();        QString week_2 = result_Obj.value("week_2").toString();        QString week_3 = result_Obj.value("week_3").toString();        QString week_4 = result_Obj.value("week_4").toString();        qDebug() << datetime_1 << datetime_2;        qDebug() << week_1 << week_2 << week_3 << week_4;    }    return 0;}
示例字符串6:全国城市油价信息

这种格式的字符串也是标准的JSON字符串,[]表示数组,这个数组内包含了5个字符串:

代码语言:javascript复制
["北京", "6.78", "7.21", "8.19", "6.45"]

但是这种不是,{}表示对象:

代码语言:javascript复制
{"北京", "6.78", "7.21", "8.19", "6.45"}

一个数组内包含5个元素,每个元素又是一个数组,每个数组又包含5个字符串,属于数组的嵌套:

代码语言:javascript复制
{    "status": "0",    "msg": "全国各省份汽柴油价格信息",    "update": "2019-07-21",    "data": [        ["地区", "92号", "95号", "98号", "0号柴油"],        ["北京", "6.78", "7.21", "8.19", "6.45"],        ["上海", "6.74", "7.17", "7.87", "6.39"],        ["江苏", "6.75", "7.18", "8.06", "6.37"],        ["天津", "6.77", "7.15", "8.07", "6.41"]    ],    "About": "wcc",    "Home": "www.wangchaochao.top"}

字符串6解析函数

代码语言:javascript复制
//解析数组嵌套的JSON字符串int Parse_Oil_Price_Json(void){    QJsonParseError err_rpt;    QJsonDocument  root_Doc = QJsonDocument::fromJson(oil_price_json, &err_rpt);//字符串格式化为JSON    if(err_rpt.error != QJsonParseError::NoError)    {        qDebug() << "JSON格式错误";        return -1;    }    else    //JSON格式正确    {        //        qDebug() << "JSON格式正确:n" << root_Doc;        QJsonObject root_Obj = root_Doc.object();
        QString msg = root_Obj.value("msg").toString();        QString update = root_Obj.value("update").toString();        QString status = root_Obj.value("status").toString();        QString About = root_Obj.value("About").toString();        QString Home = root_Obj.value("Home").toString();        qDebug() << msg << update << status << About << Home;
        /* data键解析 */        QJsonValue data_Value = root_Obj.value("data");        if(data_Value.isArray())    //数组中包含5个数组,每个数组5个字符串元素        {            for(int idx = 0; idx <= 4; idx  )            {                QJsonValue price_Obj = data_Value.toArray().at(idx);                if(price_Obj.isArray())                {                    QString str1 = price_Obj.toArray().at(0).toString();// 每个元素是一个字符串                    QString str2 = price_Obj.toArray().at(1).toString();                    QString str3 = price_Obj.toArray().at(2).toString();                    QString str4 = price_Obj.toArray().at(3).toString();                    QString str5 = price_Obj.toArray().at(4).toString();                    qDebug() << str1 << str2 << str3 << str4 << str5 ;                }            }        }    }    return 0;}

参考资料

  • JSON官方网站:

http://www.json.org/json-zh.html

总结

JSON解析配合API接口,就可以实现一些有趣的工具了,如全国油价查询工具,全国天气查询,空气质量查询,黄历查询,生活指数等等实用工具的开发。

分享几个免费的API提供平台:

  • K780:

http://www.k780.com/api

  • 天气API:

http://api.help.bj.cn/api/

  • 心知天气:

https://www.seniverse.com/

  • 和风天气:

http://www.heweather.com

  • 聚合数据:

https://www.juhe.cn/

历史精选

  • 使用cJSON库解析和构建JSON字符串
  • 【2019北京国际消费电子博览会】参观总结
  • Qt实现软件自动更新的一种简单方法
  • Qt小项目之串口助手控制LED

0 人点赞