Comparison of JSON parsing scheme under QT

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.