这两天要为产品添加一个天气预报的功能,用的是国家气象局的API,获取的信息是JSON,需要对其进行解析,以获取相应的信息。
Qt 提供的QtScript 类可以帮你解决。
首先,在pro文件中:
QT += script
然后包含头文件:
#include <QtScript/QScriptValue>
#include <QtScript/QScriptEngine>
QString code = reply->readAll(); //从国家气象局读取天气预报信息
QScriptValue sc;
QScriptEngine engine;
sc = engine.evaluate("(" + code + ")"); //规定写法,具体没研究
this->ui->CityLabel->setText(sc.property("weatherinfo").property("city").toString()); //武汉
this->ui->DateLabel->setText(sc.property("weatherinfo").property("date_y").toString()); //日期
从气象局获取的JSON类容大致如下:
{"weatherinfo":{"city":"武汉","city_en":"wuhan","date_y":"2013年5月16日",.............}}
具体怎么添加天气预报,有需要的可以留言,再详细讲