使用RapidJSON迭代并检索JSON中的嵌套对象 [英] iterate and retrieve nested object in JSON using rapidjson

查看:596
本文介绍了使用RapidJSON迭代并检索JSON中的嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析一个类似于以下内容的JSON结构

I am parsing a JSON structure which is similar as follows

{
    "item1" : "value1"
    "item2" : "value2"
    // ...
    "itemn" : {
        "outernestedItem1" : {
            "innerNestedItem1" : "valuen1"
            "innerNestedItem2" : "valuen2"
        }
        // ....
        "outernestedItemn" : {
            "innerNestedItem1" : "valuen1"
            "innerNestedItem2" : "valuen2"
        }
    }
}

外部嵌套项的数量不是固定的,因此我使用了来自Rapidjson的迭代器进行迭代,内部嵌套的对象变量是固定的,因此可以使用[]进行访问.

The number of outer nested items is not fixed, so I was iterating using iterator from rapidjson, inner-nested objects variables are fixed, so I can get access to them using [].

const rapidjson::Value& itemn = document["itemn"];
for (rapidjson::Value::ConstMemberIterator itr = itemn.MemberBegin();
itr != itemn.MemberEnd(); ++itr)
{
    rapidjson::StringBuffer sb;
    rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
    itr->value.Accept(writer);

    std::cout << sb["innerNestedItem1"].GetString();
    std::cout << sb["innerNestedItem2"].GetString();
}

但是sb(字符串缓冲区)不允许[],我知道该怎么做?

but [] is not allowed with sb(string buffer), any idea how can I do this?

我这样做的效率很低,但是只是共享解决方案,所以它可能会帮助某人提出有效的解决方案.

I did it in very inefficient way, but just sharing the solution, so it might help someone to come up with efficient solution.

const rapidjson::Value& itemn = document["itemn"];
for (rapidjson::Value::ConstMemberIterator itr = itemn.MemberBegin();
itr != itemn.MemberEnd(); ++itr)
{
    rapidjson::StringBuffer sb;
    rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
    itr->value.Accept(writer);

    //changed from here onwards
    rapidjson::Document for_outer_nested_item;
    std::string temp = sb.GetString();
    char buffer2[100000];
    strcpy_s(buffer2, temp.c_str());
    for_outer_nested_item.ParseInsitu(buffer2);
    std::cout << executive_command["innerNestedItem1"].GetString() << std::endl;
    std::cout << executive_command["innerNestedItem2"].GetString() << std::endl;
}

推荐答案

首先,让我在链接

第二个-这是我为我的项目所做的:

Second-- here's what I did for my project:

rapidjson::Document document;
// document holds a json document retrieved from a http GET request
// I did not include all of that in this example.  I am only showing
// the part of iterating through a nested object and retrieving members.

std::vector<std::string> symbols;
// holds the values I retrieve from the json document

if (document.Parse<0>( symbol.c_str() ).HasParseError() )
    Log() << "ERROR: encountered a JSON parsing error" << std::endl;
else {
    // Get the nested object that contains the elements I want.
    // In my case, the nested object in my json document was results
    // and the values I was after were identified as "t"
    rapidjson::Value& results = document["results"];
    assert(results.IsArray());
    for (rapidjson::SizeType i = 0; i < results.Size(); i++) {
        // Store the value of the element in a vector.
        symbols.emplace_back(results[i]["t"].GetString());
}                            

我认为这是一种非常干净/有效的方法.

I think this is a pretty clean/efficient approach.

这篇关于使用RapidJSON迭代并检索JSON中的嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆