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 QJsonObject
to access the data.
The QJsonObject
class works as a map of QJsonValue
objects, each of which can be converted to their fundamental types...