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 ...

Get JavaScript JSON Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.