O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required