Generating JSON using Qt
The QJsonDocument
class also has the toJson
method, which converts the object it's referencing to JSON.
How to do it…
Here's an example that converts from JSON and back to JSON, pretty-printing the JSON along the way:
QString json = "{ 'call': 'kf6gpe-7', 'lat': 37.40150, 'lng': -122.03683, 'result': 'ok'}"; QJsonDocument document = QJsonDocument.fromJson(json); QJsonObject data = document.object; QByteArrayprettyPrintedJson = document.toJson(QJsonDocumented::Indented);
How it works…
The QJsonDocument
class has a method, toJson
, which converts the document or array it's referencing to JSON. You can ask for a pretty-printed version of the JSON by passing QJsonDocument::Indented
, or a compact version of the JSON by passing QJsonDcoument::Compact
.
See also
For more information on QJsonDocument
, see the Qt documentation at http://doc.qt.io/qt-5/qjsondocument.html.