JSON generation and parsing of QT

Source: Internet
Author: User
Tags tojson

JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of JavaScript (standard ECMA-262 3rd edition-december 1999). JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate.

Common JSON Libraries
    • Jsoncpp
Jsoncpp is a development package that C + + uses to process JSON data. Website: http://jsoncpp.sourceforge.net/.
    • Cjson
Cjson is a super lightweight, easy to carry, single file, simple can be used as the ANSI-C standard JSON parser. Website:http://sourceforge.net/projects/cjson/.
    • Qjson
Qjson is a QT-based development package used to parse JSON data into Qvariant objects, and JSON arrays are mapped to qvariantlist instances, while others are mapped to Qvariantmap instances. Website: http://qjson.sourceforge.net/.about QT in theJSON Generation and parsing, Qt5 Previous versions, need to be downloaded separately, compiled, in order to use. To Qt5, a specialized qjsondocument class is provided to read and write JSON documents. the JSON in the QT5 Generation and parsing
    • Qjsondocument
The qjsondocument can be encoded from a text-based UTF-8, and can be read and written from QT's own binary format. The JSON document can be converted from its text-based representation using Qjsondocument::fromjson () to Qjsondocument, using. ToJSON () to convert it back to text. The parser is very fast and efficient, converting JSON to binary representations.
    • Qjsonobject
The Qjsonobject class is used to encapsulate JSON objects. A JSON object is a key-value pair where the key is a unique string whose value is represented by Qjsonvalue. An qjsonobject can be converted/converted from Qvariantmap.
    • Qjsonarray
The Qjsonarray class is used to encapsulate JSON arrays. A JSON array list value. This list can be manipulated by inserting and removing qjsonvalue from the array.   A qjsonarray can be converted from qvariantlist to/from. Qjsondocument valid parsed documents can be judged using!isnull (). Use IsArray () and IsObject () to determine whether an array or an object is included. An array or an object contained in a document can be retrieved using an array () or objects (), and then read or manipulated. Example Qjsonobject(1) Generate Jsonqjsonobject Json;json.insert ("name", QString ("Qt")) Json.insert ("version", 5); Json.insert ("Windows", True );  qjsondocument Document;document.setobject (JSON); Qbytearray Byte_array =document.tojson (qjsondocument::compact); QString json_str (Byte_array);  result: json_str:{"name": "Qt", "Version": 5, "Windows": true}  (2) analytic jsonqjsonparseerror json_error; Qjsondocument parse_doucment =qjsondocument::fromjson (byte_array,&json_error);  if (Json_error.error = = Qjsonparseerror::noerror)  {    if (Parse_doucment.isobject ())      {        qjsonobject obj =parse_doucment.object ();        if (obj.contains ("name") )         {    qjsonvalue name_value =obj.take ("name");    if (Name_value.isstring ())     {       qstring name =name_ Value.tostring ();    }} if (Obj.contains ("version"))  {    qjsonvalue Version_value =obj.take ("version");   if (version_value.isdouble ())     {int Version = Version_value.tovariant (). ToInt ();    }} if (Obj.contains (" Windows ") {   qjsonvalue version_value =obj.take (" Windows ");    if (Version_ Value.isbool ())     BOOL flag = Version_value.tobool ();   } }   } }     Results: Name:qtversion:5windows:true Qjsonarray(1) Generate JSONQjsonarray json;json.insert (0, QString ("Qt")), Json.insert (1, QString ("version")), Json.insert (2, true); Qjsondocument Document;document.setarray (JSON); Qbytearray Byte_array =document.tojson (qjsondocument::compact); QString json_str (Byte_array); Result: json_str:["Qt", "version", True](2) parsing JSONQjsonparseerror Json_error; Qjsondocument parse_doucment =qjsondocument::fromjson (byte_array,&json_error); if (Json_error.error ==qjsonparseerror::noerror) {if (Parse_doucment.isarray ()) {Qjsonarray array =parse_doucment . Array ();   int size = Array.size (); for (int i=0; I {qjsonvalue value =array.at (i);    if (value.isstring ()) {QString name = Value.tostring ();    } elseif (Value.isbool ()) {BOOL flag = Value.tobool (); }}} Results:array values for different subscripts0:QT1:version2:true As above, a brief introduction of the commonly used JSON library and QT in the generation and interpretation of JSON, for more information please refer to the official documents, or that sentence, there is no better than the assistant, more professional information!

JSON generation and parsing of 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.