Retrieving a nested object inside a JSON string using rapidjson Retrieving a nested object inside a JSON string using rapidjson json json

Retrieving a nested object inside a JSON string using rapidjson


You need to iterate through object's members manually, as GetString() only works on string members, while document["a"] is an Object. You need to iterate through that object's members using MemberIterator variable. I had no practice in C* for more than 15 years, so I can only give a general idea of how it should work:

for (MemberIterator m = document["a"].MemberBegin(); m != document["a"].MemberEnd(); ++m) {    std::cout << m.name << " " << (m.IsNumber()?m.GetNumber():m.GetString()) << endl;}

Also, you might want to look at Accept() method, it seems to return a JSON string of an object you give it.


If element is an object you can just access subproperties with []:

for (SizeType i = 0; i < layers.Size(); i++){     cout << layers[i]["name"].GetString() << endl;}


There is another great approach realized in rapidjson - JSON Pointers. They have experimental status and according to the documentation shall be included in v.1.1. Anyway this approach looks like XPATH for XML so to get nested value we can use syntax like

Value* tmpValue = GetValueByPointer(doc, "/result/job/blob");

I tried this functionality and in my opinion this is better than iterators.