In the official online document for QT5, qt5 guys say that there is JSON support in Qt5 and all the related API description are listed so any programmer can seek them but no useful example or tutorials are there. I believe they should put more guides how to read and write JSON data sooner or later. For those who seeks any one tutorial, here comes very basic and little code snippet below to show how to read a JSON data.







If you run the above code, then u will see something like as below


QJsonValue(object, QJsonObject({"Alter": 42,"Hobbys": ["Reiten","Golfen","Lesen"],"Kinder": [],"Name": "Mustermann","Partner": null,"Vorname": "Max","männlich": true}) )

"QJsonObject of Inhaber: " QJsonObject({"Alter": 42,"Hobbys": ["Reiten","Golfen","Lesen"],"Kinder": [],"Name": "Mustermann","Partner": null,"Vorname": "Max","männlich": true})

"QJsonObject[Hobbys] of Inhaber: " QJsonValue(array, QJsonArray(["Reiten","Golfen","Lesen"]) )

"QJsonObject of Inhaber/Hobbys: " "Reiten"


Description:

The above JSON document has 5 QJsonObject and each object has a key&value pair. 

To retrieve an object with key "Inhaber" from the QJsonDocument, then we can use d.object().value(QString("Inhaber")).toObject().

if an object have only value without key, then you can refer it as QJsonArray and use the operator [] to access its array.


Hope this will give you any hint how to use JSON support with Qt5.

 

Posted by kevino
,