[Interest] iterate through JSON data

Larry Martell larry.martell at gmail.com
Tue May 17 00:24:08 CEST 2016


I am reading in a JSON file that looks like this:

{
   "capData": {
      "host": "foo.bar.com",
      "initial_port": "8000"
      "secondary_port": "8001"
   },
   "django": {
      "host": "baz.bar.com",
      "port": "8004"
   }
}

I want to iterate through it and put the data in some structure that I
can access like this:

d['capData'].host
d['django'].port

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?



More information about the Interest mailing list