JSON generation and parsing of QT

Source: Internet
Author: User
Tags compact 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 theJsongeneration 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 ())     {  &NBsp;     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);  results: 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  & Nbsp;     {    qjsonvalue value = array.at (i);     if (Value.isstring ())     {qstring name = Value.tostring ();     }     else if (Value.isbool ())     {bool flag = Value.tobool ();    }}    }}  Result: The values corresponding to the array subscript are different 0:qt1:version2:true       as above, briefly introduce the common 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!   Note:Technology lies in communication, communication, reproduced please specify the source and maintain the integrity of the work. Originally from: http://blog.sina.com.cn/s/blog_a6fb6cc90101gnxm.html

JSON generation and parsing of QT

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.