2

I am using RapidJSON to parse JSON data except I can't work out how to loop through the members of:

{

"members":{
    "0":{
        "template":"this is member 1"
    },
    "1":{
        "template":"this is member 2"
    }
}
}

I tried the following e_doc["members"][iString]["template"].GetString() inside a loop with converting the loop index (i) to a string but it doesn't recognize it as a string.

It works as:

printf("%s", e_doc["members"]["0"]["template"].GetString());
printf("%s", e_doc["members"]["1"]["template"].GetString());
user2131737
  • 21
  • 1
  • 3

1 Answers1

7

There might be a small issue as you are not iterating over an array, but over an object. However, in the end the code is similar.

const rapidjson::Value& membersObject = e_doc["members"];
for(rapidjson::Value::ConstMemberIterator it=membersObject.MemberBegin(); it != membersObject.MemberEnd(); it++) {
   std::cout << it->value["template"].GetString();
}
einverne
  • 6,454
  • 6
  • 45
  • 91
grundprinzip
  • 2,471
  • 1
  • 20
  • 34