[Interest] iterate through JSON data

Hamish Moffatt hamish at risingsoftware.com
Tue May 17 01:53:55 CEST 2016


On 17/05/16 08:24, Larry Martell wrote:
> I've see a lot of code that does something like this:
>
>      QJsonDocument document = QJsonDocument::fromJson(jsonData);
>      QJsonObject object = document.object();
>      QJsonValue value = object.value("capData");
>
> But I don't want to hard code the keys - I want to pick up and store
> whatever happens to be in the JSON file. How can I iterate through it
> and do that?
>
> Also, what would be the best object to store this in so I can later
> access it as I want to?
>

QJsonObject has iterator methods, and for each value you can check if it 
is itself an array or an object or a simple type (QJsonValue::isArray(), 
isObject(), isString() etc).

As for another object, I can't think of another with this flexibility. 
Why not leave it in the QJson* objects?

Hamish



More information about the Interest mailing list