QJson

the easiest way for managing JSON objects with Qt

API

The full documentation of QJson can be found here.

Converting JSON to QVariant

Converting JSON's data to QVariant instance is really simple:

          // create a Parser instance
          QJson::Parser parser;

          bool ok;

          // json is a QString containing the data to convert
          QVariant result = parser.parse (json, &ok);
        

Suppose you're going to convert this JSON data:

          {
            "encoding" : "UTF-8",
            "plug-ins" : [
                "python",
                "c++",
                "ruby"
                ],
            "indent" : { "length" : 3, "use_space" : true }
          }
        

The following code would convert the JSON data and parse it:

          QJson::Parser parser;
          bool ok;

          QVariantMap result = parser.parse (json, &ok).toMap();
          if (!ok) {
            qFatal("An error occurred during parsing");
            exit (1);
          }

          qDebug() << "encoding:" << result["encoding"].toString();
          qDebug() << "plugins:";

          foreach (QVariant plugin, result["plug-ins"].toList()) {
            qDebug() << "\t-" << plugin.toString();
          }

          QVariantMap nestedMap = result["indent"].toMap();
          qDebug() << "length:" << nestedMap["length"].toInt();
          qDebug() << "use_space:" << nestedMap["use_space"].toBool();
        

The output would be:

          encoding: "UTF-8"
          plugins:
            - "python"
            - "c++"
            - "ruby"
          length: 3
          use_space: true
        

Converting QVariant to JSON

Converting a QVariant instance into a JSON object is really simple:

          QVariantList people;

          QVariantMap bob;
          bob.insert("Name", "Bob");
          bob.insert("Phonenumber", 123);

          QVariantMap alice;
          alice.insert("Name", "Alice");
          alice.insert("Phonenumber", 321);

          people << bob << alice;

          QJson::Serializer serializer;
          QByteArray json = serializer.serialize(people);

          qDebug() << json;
        

The output will be:

          "[ { "Name" : "Bob", "Phonenumber" : 123 }, { "Name" : "Alice", "Phonenumber" : 321 } ]"
        

QObjct serialization and vice-versa

It's also possible to serialize a QObject instance to JSON. All the class attributes defined as properties will be serialized. Suppose the declaration of Person class looks like this:

          class Person : public QObject
          {
            Q_OBJECT

            Q_PROPERTY(QString name READ name WRITE setName)
            Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber)
            Q_PROPERTY(Gender gender READ gender WRITE setGender)
            Q_PROPERTY(QDate dob READ dob WRITE setDob)
            Q_ENUMS(Gender)

          public:
              Person(QObject* parent = 0);
              ~Person();

              QString name() const;
              void setName(const QString& name);

              int phoneNumber() const;
              void setPhoneNumber(const int  phoneNumber);

              enum Gender {Male, Female};
              void setGender(Gender gender);
              Gender gender() const;

              QDate dob() const;
              void setDob(const QDate& dob);

            private:
              QString m_name;
              int m_phoneNumber;
              Gender m_gender;
              QDate m_dob;
          };
        

The following code will serialize an instance of Person to JSON:

          Person person;
          person.setName("Flavio");
          person.setPhoneNumber(123456);
          person.setGender(Person::Male);
          person.setDob(QDate(1982, 7, 12));

          QVariantMap variant = QObjectHelper::qobject2qvariant(&person);
          Serializer serializer;
          qDebug() << serializer.serialize( variant);
        

The generated output will be:

          { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
        

It's also possible to initialize a QObject using the values stored inside of a QVariantMap. Suppose you have the following JSON data stored into a QString:

          { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
        

The following code will initialize an already allocated instance of Person using the JSON values:

          Parser parser;
          QVariant variant = parser.parse(json);

          Person person;
          QObjectHelper::qvariant2qobject(variant.toMap(), &person);