QT的使用心得(二)Json解析与生成
QT的使用心得(二)Json解析与生成
前言
长话短说,JSON作为非常常见的标记语言,与之齐名的也就XML和YML了。后面俩先不谈,我们今天就来详细解析QT对于JSON的操作。
Json解析
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
以上三个是QT对JSON操作的三个库。
示例JSON(转自https://www.cnblogs.com/whik/p/11234889.html)
{
"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"
}
}]
}
示例C++代码
void readJsonParamter(QByteArray info)
{
QJsonParseError err_rpt;
//字符串格式化为JSON
QJsonDocument root_Doc = QJsonDocument::fromJson(info, &err_rpt);
//转Json对象失败
if(err_rpt.error != QJsonParseError::NoError)
{
qDebug() << "JSON格式错误";
}
//转Json对象成功 进行处理
else
{
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() << "解析完成!";
}
}
我贴的原贴中作者的范例很多,不再赘述。
特别提议一下**[]**这种没有关键字的Array的解析:
{
"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"
}
就像上面的data部中的内容一样:
//解析数组嵌套的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 生成
void Create_Simple_JSON(void)
{
//创建JSON对象
QJsonObject root_Obj;
//添加键值对,值的类型自动识别,顺序不可自定义
root_Obj.insert("CSDN", "https://blog.csdn.net");
root_Obj.insert("cnblogs", "https://home.cnblogs.com");
root_Obj.insert("Github", "https://github.com");
root_Obj.insert("status", 1);
root_Obj.insert("enable", true);
root_Obj.insert("update_time", "20201221");
//创建Json文档
QJsonDocument root_Doc;
root_Doc.setObject(root_Obj);
QByteArray root_str = root_Doc.toJson(QJsonDocument::Compact); //紧凑格式
QString strJson(root_str);
qDebug() << strJson;
}
结果如下:
{
"CSDN": "https://blog.csdn.net",
"Github": "https://github.com",
"cnblogs": "https://home.cnblogs.com",
"enable": true,
"status": 1,
"update_time": "20201221"
}
总结一下
QT对JSON非常友好,使用QT的库,可以轻松的解析和生成Json文件。
感谢whik的文章。