rapidjson extract key and value

流过昼夜 提交于 2019-12-01 04:08:23

问题


I'm trying to extract the key and the value of an object in array but don't find the proper getter:

 for (Value::ConstValueIterator itr = document["params"].Begin(); itr != document["params"].End(); ++itr)
{
    for (Value::MemberIterator m = itr->MemberBegin(); m != itr->.MemberEnd(); ++m) {

    }       
}

in the second loop, I want to extract the key and value from the iterator separately. how to do the extraction?


回答1:


Suppose V is a JSON object which has key-value object. You can retrieve data like this.

const rapidjson::Value& V;
for (Value::ConstMemberIterator iter = V.MemberBegin(); iter != V.MemberEnd(); ++iter){
    printf("%s\t", iter->name.GetString());
    printf("%s\t", iter->value.GetString());
}



回答2:


m is Member*, where Member is

struct Member { 
    GenericValue<Encoding, Allocator> name;     //!< name of member (must be a string)
    GenericValue<Encoding, Allocator> value;    //!< value of member.
};

Thus the proper getter for the key is m->name.

That much is obvious from "rapidjson/document.h". I can't test it further without a self-contained example (https://stackoverflow.com/help/mcve, http://www.sscce.org/).



来源:https://stackoverflow.com/questions/23887079/rapidjson-extract-key-and-value

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