注意!Qt的Json库解析有问题,请服务端转好string再过来
"{"total":95,"rows":[{"Id":"201410101329369922464abad205a9f","MyTexts":"短信中心服务号码","CodeNumber":"0x00 0x01","CodeName":"","DataLength":"16"},
这样过来可以解析
"{"total":95,"rows":[{"Id":"201410101329369922464abad205a9f","MyTexts":"短信中心服务号码","CodeNumber":"0x00 0x01","CodeName":"","DataLength":16},
这样过来不可以解析
使用到了如下的库。
#include <QJsonParseError>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QString>
#include <QJsonArray>
封装及使用方法如下
使用:使用了串行的方法调用,处于常见的查询是后面使用数据的基础的考虑。(eventloop)
void QJsonTest::btn10_clicked()
{
static WebQuestJsonEngine engine("http://192.168.1.187/LocalData/CodeSettingData");
//static WebQuestJsonEngine engine(ui.lineEdit->text());
WebQusetJsonObject objects = engine.select();
for (int i=0;i<objects.size();i++)
{
QString strCodeName = objects.at(i,"Id").toString();
QString strdatalen = objects.at(i,"MyTexts").toString();
QString strdatatype = objects.at(i,"CodeNumber").toString();
QString strCodeName1 = objects.at(i,"CodeName").toString();
QString strdatalen1 = objects.at(i,"DataLength").toString();
}
engine.releaseSelect();
}
封装:
#pragma once
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QString>
#include <QTimer>
#include "WebQusetJsonObject.h"
class WebQuestJsonEngine
: public QObject
{
Q_OBJECT
public:
WebQuestJsonEngine(QString url);
~WebQuestJsonEngine(void);
/// 建立查询
WebQusetJsonObject select();
/// 释放查询
void releaseSelect();
private:
/// post操作
QNetworkReply* do_post(QString url, QString _content);
/// get操作
void do_get(QString url, QString content);
/// json封装
void do_json( QByteArray bjson );
private slots:
private:
static QNetworkAccessManager netManager;
QJsonArray jarray;
QString url;
QTimer synchronous;
QTimer timeout;
};
#include "WebQuestJsonEngine.h"
#include <QByteArray>
#include <QNetworkRequest>
#include <QUrl>
#include <QJsonParseError>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QEventLoop>
QNetworkAccessManager WebQuestJsonEngine::netManager;
WebQuestJsonEngine::WebQuestJsonEngine(QString _url):QObject(NULL),url(_url)
{
}
WebQuestJsonEngine::~WebQuestJsonEngine(void)
{
}
//post操作
QNetworkReply* WebQuestJsonEngine::do_post(QString url, QString _content)
{
QByteArray content;
content.append(_content);
int contentLength = content.length();
QNetworkRequest req;
req.setUrl(QUrl(url));
req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
req.setHeader(QNetworkRequest::ContentLengthHeader,contentLength);
return netManager.post(req,content);
}
//get操作
void WebQuestJsonEngine::do_get(QString url, QString content)
{
url = url + content;
QNetworkRequest req;
req.setUrl(QUrl(url));
netManager.get(req);
}
//json封装
void WebQuestJsonEngine::do_json( QByteArray bjson )
{
QJsonParseError json_error;
QJsonDocument parse_doucment = QJsonDocument::fromJson(bjson, &json_error);
if(json_error.error == QJsonParseError::NoError)
{
if(parse_doucment.isObject())
{
QJsonObject obj = parse_doucment.object();
//读取内容
if( obj.contains("total") && obj.contains("rows") )
{
QJsonValue rowvalue = obj.take("rows");
if (rowvalue.isArray())
{
//保存json数组结果
jarray = rowvalue.toArray();
}
}
}
}
}
//取得操作对象
WebQusetJsonObject WebQuestJsonEngine::select()
{
//异步转同步
QEventLoop eventLoop;
connect(&netManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QNetworkReply *reply = do_post(url,"");
eventLoop.exec(); //block until finish
disconnect(&netManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QByteArray byteArray = reply->readAll();
reply->close();
do_json(byteArray);
qDebug()<<byteArray;
return WebQusetJsonObject(jarray);
}
//释放对象
void WebQuestJsonEngine::releaseSelect()
{
}