1 JSON数据简介
JSON(javascript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
JSON解析一定要掌握的两条规则:
1.如果看到是{ }–>使用JSONObject
2.如果看到的[ ]–>使用JSONArray解析
2 Qt中的JSON操作
3 DEMO
以和风天气的常规天气数据集合为例来解析JSON数据,原始的JSON数据显示到textEdit中
可以将原始的JSON数据复制到 http://www.bejson.com/ 进行解析,解析的结果如下:
{
"HeWeather6":[
{
"basic":{
"cid":"CN101050116",
"location":"道外",
"parent_city":"哈尔滨",
"admin_area":"黑龙江",
"cnty":"中国",
"lat":"45.78453827",
"lon":"126.64883423",
"tz":"+8.00"
},
"update":{
"loc":"2018-09-26 15:45",
"utc":"2018-09-26 07:45"
},
"status":"ok",
"now":{
"cloud":"25",
"cond_code":"100",
"cond_txt":"晴",
"fl":"18",
"hum":"52",
"pcpn":"0.0",
"pres":"1022",
"tmp":"19",
"vis":"10",
"wind_deg":"122",
"wind_dir":"东南风",
"wind_sc":"1",
"wind_spd":"5"
},
"daily_forecast":[
{
"cond_code_d":"100",
"cond_code_n":"100",
"cond_txt_d":"晴",
"cond_txt_n":"晴",
"date":"2018-09-26",
"hum":"61",
"mr":"18:18",
"ms":"06:07",
"pcpn":"0.0",
"pop":"2",
"pres":"1024",
"sr":"05:26",
"ss":"17:21",
"tmp_max":"20",
"tmp_min":"6",
"uv_index":"6",
"vis":"20",
"wind_deg":"193",
"wind_dir":"西南风",
"wind_sc":"3-4",
"wind_spd":"20"
},
{
"cond_code_d":"101",
"cond_code_n":"100",
"cond_txt_d":"多云",
"cond_txt_n":"晴",
"date":"2018-09-27",
"hum":"61",
"mr":"18:44",
"ms":"07:13",
"pcpn":"0.0",
"pop":"25",
"pres":"1023",
"sr":"05:27",
"ss":"17:19",
"tmp_max":"20",
"tmp_min":"8",
"uv_index":"3",
"vis":"20",
"wind_deg":"170",
"wind_dir":"南风",
"wind_sc":"3-4",
"wind_spd":"12"
},
{
"cond_code_d":"305",
"cond_code_n":"101",
"cond_txt_d":"小雨",
"cond_txt_n":"多云",
"date":"2018-09-28",
"hum":"70",
"mr":"19:14",
"ms":"08:20",
"pcpn":"6.0",
"pop":"80",
"pres":"1018",
"sr":"05:29",
"ss":"17:17",
"tmp_max":"18",
"tmp_min":"10",
"uv_index":"2",
"vis":"20",
"wind_deg":"160",
"wind_dir":"东南风",
"wind_sc":"3-4",
"wind_spd":"12"
}
],
"lifestyle":[
{
"type":"comf",
"brf":"舒适",
"txt":"白天不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。"
},
{
"type":"drsg",
"brf":"较舒适",
"txt":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"
},
{
"type":"flu",
"brf":"较易发",
"txt":"天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"
},
{
"type":"sport",
"brf":"较适宜",
"txt":"天气较好,但因风力稍强,户外可选择对风力要求不高的运动,推荐您进行室内运动。"
},
{
"type":"trav",
"brf":"适宜",
"txt":"天气较好,风稍大,但温度适宜,是个好天气哦。适宜旅游,您可以尽情地享受大自然的无限风光。"
},
{
"type":"uv",
"brf":"中等",
"txt":"属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"
},
{
"type":"cw",
"brf":"较适宜",
"txt":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"
},
{
"type":"air",
"brf":"良",
"txt":"气象条件有利于空气污染物稀释、扩散和清除,可在室外正常活动。"
}
]
}
]
}
代码如下
.pro文件
#-------------------------------------------------
#
# Project created by QtCreator 2018-09-21T21:19:38
#
#-------------------------------------------------
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = myHTTP
TEMPLATE = app
SOURCES += main.cpp\\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QNetworkAccessManager>
#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonValue>
#include <QDebug>
#include <QJsonArray>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void DecodeJson(QString JsonData); //解析Json数据
QString cid;
QString location;
QString parent_city;
QString loc;
QString status;
QString cond_code_d;
QString cond_txt_d;
QString cmf;
QString air;
private:
QNetworkAccessManager *manager;
Ui::MainWindow *ui;
private slots:
void replyFinished(QNetworkReply *);
};
#endif // MAINWINDOW_H
mainwindow.h中声明了几个变量,一个用来解析Json数据的函数,一个槽函数,仅此而已。
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#define WEATHER_URL "https://free-api.heweather.com/s6/weather?location=CN101050116&key=0db7704826964aabafc351a52384225b"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
manager=new QNetworkAccessManager(this);
connect(manager,SIGNAL(finished(QNetworkReply*)),
this,SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl(WEATHER_URL)));
}
void MainWindow::replyFinished(QNetworkReply *reply)
{
QString all=reply->readAll(); //all数据只用来显示
ui->textEdit->setText(all);
DecodeJson(all);
reply->deleteLater();
}
//用来解析Jsong数据
void MainWindow::DecodeJson(QString JsonData)
{
QJsonParseError json_error;
QByteArray byteArray = JsonData.toUtf8();
//转换为JsonDoucument文档
QJsonDocument json_doucment = QJsonDocument::fromJson(byteArray, &json_error);
if(json_error.error != QJsonParseError::NoError)
{
qDebug()<<"解析数据错误"<<endl;
return ;
}
QJsonArray json_array;
if(json_doucment.object().contains("HeWeather6"))
{
qDebug()<<"HeWeather6"<<endl;
json_array=json_doucment.object().value("HeWeather6").toArray();
}
QJsonObject main_json_data=json_array.at(0).toObject();
if(main_json_data.contains("basic"))
{
QJsonObject basc_json_data=main_json_data.value("basic").toObject();
cid=basc_json_data.value("cid").toString();
location=basc_json_data.value("location").toString();
parent_city=basc_json_data.value("parent_city").toString();
qDebug()<<cid<<endl;
qDebug()<<parent_city<<endl;
}
if(main_json_data.contains("update"))
{
QJsonObject update_json_data=main_json_data.value("update").toObject();
loc=update_json_data.value("loc").toString();
qDebug()<<loc<<endl;
}
if(main_json_data.contains("status"))
{
status=main_json_data.value("status").toString();
qDebug()<<status<<endl;
}
if(main_json_data.contains("now"))
{
}
if(main_json_data.contains("daily_forecast"))
{
qDebug()<<"daily_forecast"<<endl;
QJsonArray daily_forecast_array=main_json_data.value("daily_forecast").toArray();
//Today天气
QJsonObject today_forecast=daily_forecast_array.at(0).toObject();
cond_code_d=today_forecast.value("cond_code_d").toString();
cond_txt_d=today_forecast.value("cond_txt_d").toString();
qDebug()<<cond_code_d<<endl;
qDebug()<<"cond_txt_d="<<cond_txt_d<<endl;
//Tomorrow天气
QJsonObject tomorrow_forecast=daily_forecast_array.at(1).toObject();
cond_code_d=tomorrow_forecast.value("cond_code_d").toString();
cond_txt_d=tomorrow_forecast.value("cond_txt_d").toString();
qDebug()<<cond_code_d<<endl;
qDebug()<<"cond_txt_d="<<cond_txt_d<<endl;
}
if(main_json_data.contains("lifestyle"))
{
QJsonArray lifestyle_json_array=main_json_data.value("lifestyle").toArray();
QJsonObject comf_josn_object=lifestyle_json_array.at(0).toObject();
cmf=comf_josn_object.value("txt").toString();
qDebug()<<cmf<<endl;
QJsonObject air_josn_object=lifestyle_json_array.at(7).toObject();
air=air_josn_object.value("txt").toString();
qDebug()<<air<<endl;
}
}
MainWindow::~MainWindow()
{
delete ui;
}
mian.cpp
/*
* 程序说明:
* 以和风天气的常规天气数据集合为例,解析Json数据
*/
#include "mainwindow.h"
#include <QtNetwork>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
解析出来的数据用qDebug()打印出来,可以和刚才的数据比较一下,看解析的是否正确
参考资料
和风天气 https://www.heweather.com/documents/api/s6/weather
Qt中的JSON操作 https://blog.csdn.net/Amnes1a/article/details/68957112
JSON的三种解析方式 https://www.cnblogs.com/zhujiabin/p/5498272.html
这一篇文章写的很好 https://blog.csdn.net/miaozhenzhong/article/details/52585726
JSON数据在线解析 http://www.bejson.com/
偷懒不想写了2018/9/26