问题
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).
回答1:
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;
}
来源:https://stackoverflow.com/questions/37443829/processing-arrays-of-arrays-of-integer-with-rapidjson