QT JSON string generation and parsing

Source: Internet
Author: User
Tags tojson

1 qt JSON string generation and parsing 1.1 qt JSON parsing process

(1) the string is converted to qjsondocument

Qjsonparseerror Json_error;

Qjsondocument Json_doc = Qjsondocument::fromjson (Lpc_body.c_str (), &json_error);

if (json_error.error! = qjsonparseerror::noerror)

{

Logic_error ("Parse json ERROR%d", json_error.error);

return false;

( 2 ) qjsondocument translates to Qjsonobject

Qjsonobject json_object = Json_doc.object ();

( 3 ) by Qjsonobject function to get the value

Qjsonobject json_object = Json_doc.object ();

if (!json_object.contains ("Data"))

{

Logic_info ("RECV server subscribeevent return data%s", Data.lpc_body. C_str());

return true;

}

Qjsonobject qdata= json_object["Data"].toobject ();

if (! Qdata.contains ("message"))

{

Logic_error ("Json_object can ' t find Message");

return false;

}

Qjsonobject message = qdata["message"].toobject ();

Next is the data field

if (! Message.contains ("EventData"))

{

Logic_error ("Can ' t find EventData");

return false;

}

Qjsonobject EventData = message["EventData"].toobject ();

if (! Eventdata.contains ("Alarmmessage"))

{

Logic_error ("Can ' t find Alarmmessage");

return false;

}

Qjsonobject alarmmsg = eventdata["Alarmmessage"].toobject ();

Alarmdata.stralarmname = alarmmsg["Objectivename"].tostring (). tostdstring ();

Alarmdata.strstime = alarmmsg["AlarmTime"].tostring ();

Alarmdata.streventid = alarmmsg["EventId"].tostring (). tostdstring ();

Alarmdata.stralarmlevel = alarmmsg["Eventlevel"].tostring (). tostdstring ();

Alarmdata.streventtype = alarmmsg["EventType"].tostring (). tostdstring ();

Alarmdata.stralarmlogid = alarmmsg["id"].tostring (). tostdstring ();

Logic_info ("Parase Alarm success!");

1.2 QT JSON construction process

The construction process is the opposite of the parsing process

(1) Create a Qjsonobject object and insert the various types of values through the Insert function.

Iterator Insert (const QString &key, const qjsonvalue &value);

Where Qjsonvalue is a class with multiple constructors, you can enter multiple data types. You can use various functions to determine and convert to the corresponding data type.

constructor function

Qjsonvalue (Type = Null);

Qjsonvalue (bool b);

Qjsonvalue (double n);

Qjsonvalue (int n);

Qjsonvalue (qint64 N);

Qjsonvalue (const QString &s);

Qjsonvalue (qlatin1string s);

Type judgment function

Type type () const;

inline bool IsNull () const {return type () = = Null;}

inline bool Isbool () const {return type () = = bool;}

inline bool Isdouble () const {return type () = = Double;}

inline bool Isstring () const {return type () = = String;}

inline bool IsArray () const {return type () = = Array;}

inline bool IsObject () const {return type () = = Object;}

inline bool isundefined () const {return type () = = Undefined;}

Convert output function

BOOL Tobool (BOOL DefaultValue = false) const;

int toint (int defaultvalue = 0) const;

Double ToDouble (Double defaultvalue = 0) const;

QString toString () const;

QString toString (const QString &defaultvalue) const;

Qjsonarray ToArray () const;

Qjsonarray ToArray (const qjsonarray &defaultvalue) const;

Qjsonobject toobject () const;

Qjsonobject toobject (const qjsonobject &defaultvalue) const;

Qjsonobject Json_object;

Json_object.insert ("ErrorCode", 1);//number

Json_object.insert ("Errormodule", "error_module");//String

Json_object.insert ("bool", true);

You can directly convert the corresponding data in the map to a JSON string

if (!key_value. Empty())

{

Json_object.insert ("Data", Qjsonobject::fromvariantmap (Key_value));

}

( 2 ) After organizing the structure, use qjsondocument of the SetObject function Settings Object

Qjsondocument Json_doc;

Json_doc.setobject (Json_object);

( 3 ) with qjsondocument of the Tojson function into a string

String str= Json_doc.tojson (qjsondocument::compact);

Qjson character parsing and generation This is the case, if you want to get a deeper understanding. You can go to the constructor to learn more about the interface.

QT JSON string generation and parsing

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.