Parsing JSON using Qt

The Qt implementation of JSON parsing is actually quite similar in its interface to the Android version. Qt defines the QJsonObject and QJsonArray classes, which can contain JSON maps and JSON arrays respectively. The parsing itself is done by the QJsonDocument class, which has a static fromJson method that accepts JSON and performs the necessary parsing.

How to do it…

Here's a simple example:

QString json = "{ 'call': 'kf6gpe-7', 'lat': 37.40150, 'lng': -122.03683, 'result': 'ok'}";
QJsonDocument document = QJsonDocument.fromJson(json);
QJsonObject data = document.object;
QString call = data["call"].toString();

How it works…

The parsing is two-step: first, the code parses the JSON using QJsonDocument and then uses the resulting ...

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.