Qt tip 网络请求 QNetworkRequest QJason 处理 Jason

  • 通过天气api获取当前天气
  • api来自 百度apiStore。http://apistore.baidu.com/apiworks/servicedetail/112.html
  • 需要自己申请秘钥API

    获取到的天气api数据是Jason格式的,新版的qt有专门的Jason类处理,比较方便。老版的貌似有库,可自行试试。

需要在pro文件添加

QT       += network 

头文件

#include 
#include 
#include 

发送api请求

//发送请求
void Widget::on_pushButton_clicked()
{
    manage = new QNetworkAccessManager(this);
    QNetworkRequest network_request;
    QString cityName = "上海";

    /*设置发送数据*/
    network_request.setUrl(QUrl(QString("http://apis.baidu.com/apistore/weatherservice/recentweathers?cityname=%1")
                        .arg(cityName)));
    network_request.setRawHeader("apikey", "您自己的apikey");

    /*建立connect,replyFinished为自定义槽函数,获取api发回来的天气信息(Jason)格式*/
    connect(manage,SIGNAL(finished(QNetworkReply *)),this,SLOT( getReplyFinished(QNetworkReply *)) );

    /*发送get网络请求*/
    manage->get(network_request);

}

获取api发回的数据

//获取天气api传回的数据
void Widget::getReplyFinished(QNetworkReply *reply)
{
    QJsonObject data = QJsonDocument::fromJson(reply->readAll()).object();
    qDebug() << "天气Jason:" << data;
    //ui->textBrowser->setText(data);
}

Qt tip 网络请求 QNetworkRequest QJason 处理 Jason_第1张图片

将网络请求封装成一个函数

//widget.h

QNetworkAccessManager *manage;


//widget.cpp

void Widget::setNetworkRequest(QNetworkRequest &request, QString cityName)
{
    request.setUrl(QUrl(QString("http://apis.baidu.com/apistore/weatherservice/recentweathers?cityname=%1")
                                .arg(cityName)));
    request.setRawHeader("apikey", "b446bb51d329b1098b008568231a772b");
}

//获取天气api传回的数据
void Widget::getReplyFinished(QNetworkReply *reply)
{
    QJsonObject data = QJsonDocument::fromJson(reply->readAll()).object();
    qDebug() << "天气Jason:" << data;
}


/********调用*********/
//设置发送数据
manage = new QNetworkAccessManager(this);
QString cityName = "上海";
qDebug() << "init cityName:" << cityName;

//发送请求
setNetworkRequest(network_request, cityName);
connect(manage,SIGNAL(finished(QNetworkReply *)),this,SLOT(getReplyFinished(QNetworkReply*)));

//发送get网络请求
manage->get(network_request);

  • 获取到Jason的数据处理

Jason返回数据 http://apistore.baidu.com/astore/toolshttpproxy?apiId=ucy1xA&isAworks=1
返回的Jason数据如:

{
    "errNum": 0,
    "errMsg": "success",
    "retData": {
        "city": "北京",
        "cityid": "101010100",
        "today": {
            "date": "2016-09-01",
            "week": "星期四",
            "curTemp": "25℃",
            "aqi": "31",
            "fengxiang": "北风",
            "fengli": "3-4级",
            "hightemp": "27℃",
            "lowtemp": "19℃",
            "type": "阴",
            "index": [
                {
                    "name": "感冒指数",
                    "code": "gm",
                    "index": "",
                    "details": "相对于今天将会出现大幅度降温,易发生感冒,请注意适当增加衣服,加强自我防护避免感冒。",
                    "otherName": ""
                },
                {
                    "code": "fs",
                    "details": "属弱紫外辐射天气,长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。",
                    "index": "弱",
                    "name": "防晒指数",
                    "otherName": ""
                },
                {
                    "code": "ct",
                    "details": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
                    "index": "舒适",
                    "name": "穿衣指数",
                    "otherName": ""
                },
                {
                    "code": "yd",
                    "details": "阴天,较适宜进行各种户内外运动。",
                    "index": "较适宜",
                    "name": "运动指数",
                    "otherName": ""
                },
                {
                    "code": "xc",
                    "details": "较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。",
                    "index": "较适宜",
                    "name": "洗车指数",
                    "otherName": ""
                },
                {
                    "code": "ls",
                    "details": "天气阴沉,不利于水分的迅速蒸发,不太适宜晾晒。若需要晾晒,请尽量选择通风的地点。",
                    "index": "不太适宜",
                    "name": "晾晒指数",
                    "otherName": ""
                }
            ]
        },
        "forecast": [
            {
                "date": "2016-09-02",
                "week": "星期五",
                "fengxiang": "无持续风向",
                "fengli": "微风级",
                "hightemp": "28℃",
                "lowtemp": "20℃",
                "type": "阴"
            },
            {
                "date": "2016-09-03",
                "week": "星期六",
                "fengxiang": "无持续风向",
                "fengli": "微风级",
                "hightemp": "29℃",
                "lowtemp": "20℃",
                "type": "多云"
            },
            {
                "date": "2016-09-04",
                "week": "星期天",
                "fengxiang": "无持续风向",
                "fengli": "微风级",
                "hightemp": "29℃",
                "lowtemp": "19℃",
                "type": "阵雨"
            },
            {
                "date": "2016-09-05",
                "week": "星期一",
                "fengxiang": "无持续风向",
                "fengli": "微风级",
                "hightemp": "30℃",
                "lowtemp": "21℃",
                "type": "多云"
            }
        ],
        "history": [
            {
                "date": "2016-08-25",
                "week": "星期四",
                "aqi": "31",
                "fengxiang": "北风",
                "fengli": "4-5级",
                "hightemp": "30℃",
                "lowtemp": "18℃",
                "type": "多云"
            },
            {
                "date": "2016-08-26",
                "week": "星期五",
                "aqi": "41",
                "fengxiang": "无持续风向",
                "fengli": "微风级",
                "hightemp": "29℃",
                "lowtemp": "17℃",
                "type": "晴"
            },
            {
                "date": "2016-08-27",
                "week": "星期六",
                "aqi": "73",
                "fengxiang": "无持续风向",
                "fengli": "微风级",
                "hightemp": "32℃",
                "lowtemp": "20℃",
                "type": "晴"
            },
            {
                "date": "2016-08-28",
                "week": "星期天",
                "aqi": "42",
                "fengxiang": "无持续风向",
                "fengli": "微风级",
                "hightemp": "31℃",
                "lowtemp": "18℃",
                "type": "晴"
            },
            {
                "date": "2016-08-29",
                "week": "星期一",
                "aqi": "56",
                "fengxiang": "无持续风向",
                "fengli": "微风级",
                "hightemp": "31℃",
                "lowtemp": "19℃",
                "type": "晴"
            },
            {
                "date": "2016-08-30",
                "week": "星期二",
                "aqi": "99",
                "fengxiang": "无持续风向",
                "fengli": "微风级",
                "hightemp": "30℃",
                "lowtemp": "19℃",
                "type": "多云"
            },
            {
                "date": "2016-08-31",
                "week": "星期三",
                "aqi": "43",
                "fengxiang": "北风",
                "fengli": "3-4级",
                "hightemp": "33℃",
                "lowtemp": "20℃",
                "type": "晴"
            }
        ]
    }
}

头文件

#include 
#include 
#include 

在widget.h中添加一个结构体存放天气信息

struct WeatherInfo{
    QString date;
    QString week;
    QString type;
    QString curTemp;
    QString hightemp;
    QString lowtemp;
    QString fengli;
    QString fengxiang;
    QString aqi;
};
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    void getTodayWeatherInfo(QJsonObject data);                     //获取当日天气信息

widget.cpp中将Jason处理,并且保存到结构体,打印处理。如下:

//获取当日天气信息
void Widget::getTodayWeatherInfo(QJsonObject data)
{
    QJsonObject today = data.value("retData").toObject().value("today").toObject();
    WeatherInfo todayInfo;
    todayInfo.date = today.value("date").toString();//2016-08-26
    todayInfo.week = today.value("week").toString();//星期五
    todayInfo.type = today.value("type").toString();//阵雨
    todayInfo.curTemp = today.value("curTemp").toString();//30℃
    todayInfo.hightemp = today.value("hightemp").toString();//34℃
    todayInfo.lowtemp = today.value("lowtemp").toString();//24℃
    todayInfo.fengli = today.value("fengli").toString();//微风级
    todayInfo.fengxiang = today.value("fengxiang").toString();//东北风
    todayInfo.aqi = today.value("aqi").toString();//60

    qDebug() << "getTodayWeatherInfo:\n" << todayInfo.date + todayInfo.week << todayInfo.type << todayInfo.curTemp
             << todayInfo.hightemp << todayInfo.lowtemp << todayInfo.fengli << todayInfo.fengxiang
             << todayInfo.aqi;

    QStringList todayInfoList;
    todayInfoList << todayInfo.date + todayInfo.week << todayInfo.type << todayInfo.curTemp
                  << todayInfo.hightemp << todayInfo.lowtemp << todayInfo.fengli << todayInfo.fengxiang
                  << todayInfo.aqi;
    for(int i=0; i < todayInfoList.size(); i++)
    {
        ui->textBrowser->append(todayInfoList.at(i));
    }
}

Qt tip 网络请求 QNetworkRequest QJason 处理 Jason_第2张图片

其它的比如历史七天数据,洗车啊什么的都是类似的操作。

你可能感兴趣的:(Qt,Creator,笔记)