c++qtqt5qtcoreqjson

How To Use QJson library In Qt


Now i am using Qt v2.3 I have one problem, i need to send the data to server by using json but Qjson class is not there. How to include JSON to Qt? or Is there any external header file for that?


Solution

  • You are probably referring to the QtCreator version as opposed to the Qt. Qt 2.3 would be very old as a framework version used, and probably most of the third-party libraries would not work with it anymore, anyhow.

    Just use Qt 5, and that means switch to it if you had not done so yet.

    You will then get access to the qt json classes in QtCore all of a sudden. Basically, you will have access to these classes:

    QJsonArray Encapsulates a JSON array

    QJsonDocument Way to read and write JSON documents

    QJsonObject Encapsulates a JSON object

    QJsonObject::iterator QJsonObject::iterator class provides an STL-style non-const iterator for QJsonObject

    QJsonObject::const_iterator QJsonObject::const_iterator class provides an STL-style const iterator for QJsonObject

    QJsonParseError Used to report errors during JSON parsing

    QJsonValue Encapsulates a value in JSON

    Here you can find a very good example about the usage of these classes, more or less:

    JSON Save Game Example

    If you have the Qt 4 series, or older, you could try to backport these classes if no one had not done so.

    The overall advantage of these classes is that they are extremely fast compared to other libraries, like qjson (4-5 times) or even the libjson library written in C! This is possible due to the binary format it uses internally to speed up the read and write operation.