iterate and retrieve nested object in JSON using rapidjson

后端 未结 5 1589
情书的邮戳
情书的邮戳 2021-01-12 07:20

I am parsing a JSON structure which is similar as follows

{
    \"item1\" : \"value1\"
    \"item2\" : \"value2\"
    // ...
    \"itemn\" : {
          


        
5条回答
  •  借酒劲吻你
    2021-01-12 07:57

    void parseRecursive(std::string scope
                        , rapidjson::Value::ConstMemberIterator object
                        , std::unordered_map& values)
    {
        if (scope.empty())
        {
            scope = object->name.GetString();
        }
        else
        {
            scope = scope + "::" + object->name.GetString();
        }
        auto inElement = scope + "::";
    
        if (object->value.IsObject())
        {
            for (auto it = object->value.MemberBegin(); it != object->value.MemberEnd(); ++it)
            {
                parseRecursive(scope, it, values);
            }
        }
        else if (object->value.IsDouble())
        {
            values.emplace(inElement, std::to_string(object->value.GetDouble()));
        }
        else if (object->value.IsInt())
        {
            values.emplace(inElement, std::to_string(object->value.GetInt()));
        }
        else
        {
            LOGW("Unsuported: " << inElement << object->name.GetString());
        }
    }
    

    And start with document: rapidjson::Document document;

    for (auto it = document.MemberBegin(); it != document.MemberEnd(); ++it)
    {
        parseRecursive("", it, _values);
    }
    

提交回复
热议问题