Processing arrays of arrays of integer with rapidjson

荒凉一梦 提交于 2021-01-29 05:50:52

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!