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();
推荐阅读
- 画解算法(1.|画解算法:1. 两数之和)
- ts泛型使用举例
- https请求被提早撤回
- EditText默认不获取焦点弹出键盘
- HTTP高级(Cookie,Session|HTTP高级(Cookie,Session ,LocalStorage )
- 拒绝可以很艺术,或者很行为艺术。
- whlie循环和for循环的应用
- www和https://又被Chrome地址栏隐藏了
- Spring集成|Spring集成 Mina
- 零基础学习Python作业本(13)