I’ve been started to use Json and learn to parse json ever since I started working on creating a Remember The Milk app for mobile project.

I’ve been using qjson for Qt4 and glad to find that QJson is available in Qt5. But I found that not much example is available online on how to parse json with QJson in Qt5. I decided to write an example.

The full code (main.cpp and a sample json file) is available at Gist Github. (https://gist.github.com/cckwes/6435552)  The json file is the output of Desktoppr’s API (random wallpaper).

As you can see it’s a json object (response) with some attributes like height, width, url and etc. There’s another object (image) which contains the thumbnail and preview image information.

So the parse code is simple, get the main json object as QVariantMap, and get the response as another QVariantMap. Then you can get the information, let say width using responseMap[“width”].toInt(); It’s pretty straight forward.

A friend on Twitter asked me about the performance of QJson and how is it compare to JSON.parse() in QML. I don’t know the answer at the moment but I will do a benchmark on it soon. 😉 Stay tuned