Qt通过网页API获取json数据并解析
核心代码如下:
1 QNetworkAccessManager* m_manager = new QNetworkAccessManager; 2 QNetworkRequest request; request.setUrl(QUrl(QString("http://wthrcdn.etouch.cn/weather_mini?city="))); 3 m_manager->get(request);//向网页发起get请求 4 connect(m_manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(dealMsg(QNetworkReply*)));//请求完成,获取数据并在槽函数中进行处理 1 void weather::dealMsg(QNetworkReply * reply)//对数据进行解析 2 { 3 QByteArray array = reply->readAll(); 4 QJsonParseError error; 5 QJsonDocument data = QJsonDocument::fromJson(array, &error); 6 if (!data.isNull()) { 7 QJsonObject obj = data.object(); 8 if (obj.contains("data")) { 9 QJsonValue val = obj.value("data"); 10 ...... 11 } 12 reply->deleteLater(); 13 }