About JSON parsing
There are currently several popular options:
1) Call Qscript Library Solution Board
2) Qt5 Qjsondocument Library
3) using the Qjson library
4) using the Jsoncpp library
5) Use LIBQXT Library in Qxtjson
Qscript parsing
Because I have to use both the embedded device and the desktop platform.
The first method needs to use the Qscript library, not good, just a qscript library because contains JSON,JAVASCRIPT,QML library a whole set of things, the size of itself is qtgui,qtnework,qtsql such as the sum of the library more!
See also: http://blog.chinaunix.net/uid-20737871-id-1881233.html
Qbytearray result;result = Qhttpclient->readall ();Qscriptvalue SC; Qscriptengine engine;sc = engine. Evaluate(QString (Result));//In new versions it could need to the look like Engine.evaluate ("(" + QString (Result) + ")");if (SC. Property("Result"). IsArray()) {qstringlist items;Qscriptvaluetosequence (SC. Property("Result"), items); foreach (QString str, items) {Qdebug ("Value%s"Str. tostdstring(). C_str ());} }
So the program is immature.
Qjsondocument Library
Only executed under the QT5 library, but using the more mature QT 4.8 embedded under the
voidMainwindow::replyfinished (qnetworkreply* Replay) {if(Replay->error () = = Qnetworkreply::noerror) {Qbytearray data = Replay->readall (); Qjsonparseerror Jsonerror;//QT5 New ClassQjsondocument JSON = Qjsondocument::fromjson (data, &jsonerror);//QT5 New Class if(Jsonerror.error = = Qjsonparseerror::noerror)//QT5 New Class{if(Json.isobject ()) {Qjsonobject obj = Json.object ();//QT5 New Class if(Obj.contains ("Weatherinfo")) {if(obj["Weatherinfo"].isobject ()) {Qjsonobject weatherobj = obj["Weatherinfo"].toobject (); Weathermap = Weatherobj.tovariantmap ();//qdebug () << weathermap;Showweather ();return; } } } } }}
Qjson Library
Use more people, but for embedded support is not very good, transplant complex,
Http://qjson.sourceforge.net
Jsoncpp Library
Code is small, only 4 CPP files, easy to transplant, only need to include the source files and header files in the project source code, simple adjustment can be directly used (not work)
#include "Lib/json/json.h"void Testjson () {using namespaceSTD; STD:: String strvalue ="{\" study number \ ": \" 20120207001\ ", \" name \ ": \" Zhang Mowgli \ ", \" sex \ ": \" male \ "}";Json::reader Reader;Json::value Value;if (reader. Parse(strvalue, value)) {STD:: String out= value["School Number"]. asstring();//STD:: cout << out<<STD:: Endl;QString tmp = Qstring::fromutf8 ( out. C_str ());Qdebug () << tmp; out= value["Name"]. asstring();cout << out<<STD:: Endl;TMP = Qstring::fromutf8 ( out. C_str ());Qdebug () << tmp; out= value["Gender"]. asstring();cout << out<<STD:: Endl;TMP = Qstring::fromutf8 ( out. C_str ());Qdebug () << tmp;}}
But because of the use of std::string, so that each conversion needs to be converted to qstring, other data structures are so converted, more trouble
This is a complete sample of the test: http://download.csdn.net/detail/work4blue/8728497
The best Qxtjson Solution
The file is small and only two qxtglobal.cpp and Qxtjson.cpp are available. The most critical it is the direct output is the qvariantmap,qstringlist and Qstring objects! , the performance is very high. Specific references See the
Http://libqxt.bitbucket.org/doc/0.6/qxtjson.html
QByteArray rawdata = reply->readAll(); QxtJSON parser; QVariantMap map = parser.parse(rawdata.data()).toMap(); QVariantMap map2 = map["responseData"].toMap(); QString result = map2["language"].toString();
The demo code uses the Google/bing network translation interface to translate the words, see:
http://download.csdn.net/detail/work4blue/8728153
Comparison of JSON parsing schemes under QT