JSON With Qt

The QJsonDocument class provides a way to read and write JSON documents.
isArray,isObject,isNull,isEmpty to check the main content of document
object(),array() to get main QJsonObject or QJsonArray
toJson() convert it to QByteArray (json string)
A JSON object is a list of key value pairs, where the keys are unique strings and the values are represented by a QJsonValue.
A QJsonObject can be converted to and from a QVariantMap. You can query the number of (key, value) pairs with size(), insert(), and remove() entries from it and iterate over its content using the standard C++ iterator pattern.
A JSON array is a list of values. The list can be manipulated by inserting and removing QJsonValue‘s from the array.
A QJsonArray can be converted to and from a QVariantList. You can query the number of entries with size(), insert(), and removeAt() entries from it and iterate over its content using the standard C++ iterator pattern.
A value in JSON can be one of 6 basic types:
JSON is a format to store structured data. It has 6 basic data types:bool QJsonValue::Bool
double QJsonValue::Double
string QJsonValue::String
array QJsonValue::Array
object QJsonValue::Object
null QJsonValue::Null
A value can represent any of the above data types. In addition, QJsonValue has one special flag to represent undefined values. This can be queried with isUndefined().
The type of the value can be queried with type() or accessors like isBool(), isString(), and so on. Likewise, the value can be converted to the type stored in it using the toBool(), toString() and so on.
Values are strictly typed internally and contrary to QVariant will not attempt to do any implicit type conversions. This implies that converting to a type that is not stored in the value will return a default constructed return value.
you can use fromVariant(const QVariant &variant) to QJsonValue from QVariant

  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 27,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  "phoneNumbers": [
      "type": "home",
      "number": "212 555-1234"
      "type": "office",
      "number": "646 555-4567"
  "children": [],
  "spouse": null
QFile file("json.txt");
QByteArray data=file.readAll();
QJsonObject mainObject=QJsonDocument::fromJson(data).object();
qDebug()<<"first name : "<<(*mainObject.find("firstName")).toString();
qDebug()<<"address  : "<<mainObject["address"].toObject()["streetAddress"].toString();
qDebug()<<"phone number  : "<<mainObject["phoneNumbers"].toArray()[0].toObject()["number"].toString();
first name :  "John"
address  :  "21 2nd Street"
phone number  :  "212 555-1234"