0

Looking to rapidjson documentation this code is suggested to query an array:

for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
    printf("%d ", itr->GetInt());

However, I have an array of arrays, something like:

[ [0,2], [1,2], [4, 5], ... ]

I would like to have some two-levels for for processing it, something like this:

for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
    for (Value::ConstValueIterator itr2 = itr->GetArray().Begin(); itr2 != itr->GetArray().End(); ++itr2)
        printf("%d ", itr2->GetInt());

However, it seems that itr doesn't have any GetArray() or equivalente method which returns an object in which get the second (inner) iterator.

NOTE: I have found this Q&A post, but it seems to be based in gettinig a Value representing the internal array. However, I don't know how to get such value from the itr iterator (e.g. itr->value doesn't work).

Community
  • 1
  • 1
fgalan
  • 11,732
  • 9
  • 46
  • 89

1 Answers1

2

You can parse arrays of arrays json this way:

#include <vector>
#include <rapidjson/document.h>

using namespace rapidjson;

int main(void)
{
    std::vector<std::vector<int>> vec;
    const char* json = "{ \"data\":[[0,1],[2,3]] }";

    Document d;

    d.Parse<0>(json);
    Value& data = d["data"];
    vec.resize(data.Size());

    for (SizeType i = 0; i<data.Size(); i++)
    {
        const rapidjson::Value &data_vec = data[i];
        for (SizeType j = 0; j < data_vec.Size(); j++)
            vec[i].push_back(data_vec[j].GetInt());
    }

    return 0;
}
doqtor
  • 8,414
  • 2
  • 20
  • 36