注意!Qt的Json库解析有问题,请服务端转好string再过来

注意!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()
{

}






评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值