Qt5 HTTP获取JSON数据及解析测试代码

QNetworkAccessManager manager; QNetworkRequest req; req.setUrl(QUrl( "http://localhost:52280/?c=base&a=invlist" )); req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json" ); QNetworkReply *reply = manager.post(req, QByteArray( "{}" )); QEventLoop ev; connect(reply, SIGNAL(finished()), &ev, SLOT(quit())); ev.exec(QEventLoop::ExcludeUserInputEvents); if (reply->error() == QNetworkReply::NoError) { QByteArray ba = reply->readAll(); QJsonParseError jsonpe; QJsonDocument json = QJsonDocument::fromJson(ba, &jsonpe); if (jsonpe.error == QJsonParseError::NoError) { if (json.isObject()) { QJsonObject obj = json.object(); if (obj.contains( "error" )) { qDebug() << "error:" << obj[ "error" ]; } else { QJsonArray jarray = obj[ "invlist" ].toArray(); for ( int row = 0; row < jarray.size(); row++) { qDebug() << "====Row[" << row << "] Start===================" ; QJsonObject jobj = jarray[row].toObject(); for (QJsonObject::Iterator it = jobj.begin(); it != jobj.end(); it++) { qDebug() << it.key() << " => " << it.value(); } } } } else { qDebug() << "error, shoud json object" ; } } else { qDebug() << "error:" << jsonpe.errorString(); } } else { qDebug() << "error:" << reply->errorString();

    推荐阅读