前言
在Qt开发环境下使用Json 的解析和输出当然要使用QJson来完成。
QJson解析JSON 主要使用的类如下
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
QJson 中Json 文件的生成
简单的json生成
,
"Github": "https://github.com/",
"enable": true,
"status": 1,
"update_time": "20221120"
}
代码实现
//一个简单的JSON键值对生成
QString CreateJson::Create_Simple_JSON(void)
{
//创建JSON对象
QJsonObject root_Obj;
//添加键值对,值的类型自动识别,顺序不可自定义
root_Obj.insert("Github", "https://github.com/");
root_Obj.insert("status", 1);
root_Obj.insert("enable", true);
root_Obj.insert("update_time", "20221120");
//创建Json文档
QJsonDocument root_Doc;
root_Doc.setObject(root_Obj);
//QByteArray root_str = root_Doc.toJson(QJsonDocument::Compact); //紧凑格式
QByteArray root_str = root_Doc.toJson(QJsonDocument::Indented); //标准JSON格式 QString strJson(root_str);
QString strJson(root_str);
qDebug() << strJson;
return strJson;
}
程序效果
json 中嵌套
,
"result": {
"datetime_1": "2022-07-21 10:46:57",
"datetime_2": "2022年07月21日 10时46分57秒",
"timestamp": "ok",
"week_1": "0",
"week_2": "星期日",
"week_3": "周日",
"week_4": "Sunday"
},
"status": "success"
}
代码实现
QString CreateJson::Create_BJTime_JSON(void)
{
QJsonObject result_Obj;
result_Obj.insert("timestamp", "ok");
result_Obj.insert("datetime_1", "2022-07-21 10:46:57");
result_Obj.insert("datetime_2", "2022年07月21日 10时46分57秒");
result_Obj.insert("week_1", "0");
result_Obj.insert("week_2", "星期日");
result_Obj.insert("week_3", "周日");
result_Obj.insert("week_4", "Sunday");
QJsonObject root_Obj;
//添加键值对,值的类型自动识别,顺序不可自定义
root_Obj.insert("status", "success");
root_Obj.insert("Blog"
root_Obj.insert("result", result_Obj);
//创建Json文档
QJsonDocument root_Doc;
root_Doc.setObject(root_Obj);
// QByteArray root_str = root_Doc.toJson(QJsonDocument::Compact); //紧凑格式
QByteArray root_str = root_Doc.toJson(QJsonDocument::Indented); //标准JSON格式
// qDebug() << root_str; //中文输出乱码
QString strJson(root_str);
qDebug() << strJson;
return root_str;
}
程序效果
Json 数组
{
"status": "ok",
"weather": [{
"date": "2022-07-21",
"cond_txt": "多云",
"cond_code": "101",
"hum": "23",
"tmp_H": "31",
"tmp_L": "25"
}, {
"date": "2022-07-22",
"cond_txt": "晴",
"cond_code": "100",
"hum": "20",
"tmp_H": "33",
"tmp_L": "26"
}, {
"date": "2022-07-23",
"cond_txt": "阵雨",
"cond_code": "107",
"hum": "45",
"tmp_H": "32",
"tmp_L": "25"
}],
"update": "2022-07-21 11:00",
"Blog": "www.baidu.com"
}
代码实现
QString CreateJson::Create_Array_JSON(void)
{
QJsonObject day0_Obj;
QJsonObject day1_Obj;
QJsonObject day2_Obj;
day0_Obj.insert("date", "2022-07-21");
day0_Obj.insert("cond_txt", "多云");
day0_Obj.insert("cond_code", "101");
day0_Obj.insert("hum", "23");
day0_Obj.insert("tmp_H", "31");
day0_Obj.insert("tmp_L", "25");
day1_Obj.insert("date", "2022-07-21");
day1_Obj.insert("cond_txt", "阵雨");
day1_Obj.insert("cond_code", "107");
day1_Obj.insert("hum", "44");
day1_Obj.insert("tmp_H", "30");
day1_Obj.insert("tmp_L", "26");
day2_Obj.insert("date", "2022-07-22");
day2_Obj.insert("cond_txt", "晴");
day2_Obj.insert("cond_code", "100");
day2_Obj.insert("hum", "20");
day2_Obj.insert("tmp_H", "33");
day2_Obj.insert("tmp_L", "26");
QJsonArray weather_Arr;
weather_Arr.insert(0, day0_Obj);
weather_Arr.insert(1, day1_Obj);
weather_Arr.insert(2, day2_Obj);
QJsonObject root_Obj;
root_Obj.insert("status", "ok");
root_Obj.insert("update", "2022-07-21 11:00");
root_Obj.insert("Blog", "www.wangchaochao.top");
root_Obj.insert("weather", weather_Arr); //数组作为weather键的值
//创建Json文档
QJsonDocument root_Doc;
root_Doc.setObject(root_Obj);
QByteArray root_str = root_Doc.toJson(QJsonDocument::Compact); //紧凑格式
// QByteArray root_str = root_Doc.toJson(QJsonDocument::Indented); //标准JSON格式
// qDebug() << root_str; //中文输出乱码
QString strJson(root_str);
qDebug() << strJson;
return strJson;
}
程序效果
QJson 格式的解析
格式一
解析的格式如下:
{
"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"
}
}
代码实现如下:
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;
格式二
{
"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"
}]
}
代码实现
int QJsonParse::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())
{
int nsize = data_Vaule.toArray().size();
for(int idx = 0; idx < nsize; 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;
}
总结
好了,到此就是简单的QJson的 json 文件的解析,以及json格式文件的组装。
标签:week,insert,Obj,Qt,QString,使用,root,QJson,result From: https://blog.51cto.com/u_15461374/5938259