RapidJson:如何从JSON获取所有Key_names?(cocos2dx)

Zen*_*ner 1 cocos2d-x rapidjson

从Json字符串(或文件),我想要在不事先知道密钥的情况下收集键/值对.假设我有这个Json:

{ "a":"1","b":"2","c":"3" }
Run Code Online (Sandbox Code Playgroud)

我想收集所有关键字符串"a","b","c","d"及其各自的值.BTW:我在Cocos2dX 3.3中使用rapidjson集成.任何的想法?

我现在要用的是:

rapidjson::Document JSON; 

//..... collecting the JSON .... then

for (rapidjson::Value::MemberIterator M=JSON.MemberonBegin(); M!=JSON.MemberonEnd(); M++)
{
    //..... I have access to M->name and M->value here
    //..... but I don't know how to convert them to std::string or const char*
}   
Run Code Online (Sandbox Code Playgroud)

但我坚持这一点.

Zen*_*ner 5

我刚刚发现rapidjson :: Value :: MemberIterator中有函数.这是一个枚举Json文档中的键/对的示例.此示例仅记录根密钥.您需要额外的工作来检索子密钥

const char *jsonbuf = "{\"a\":\"1\",\"b\":\"2\",\"c\":\"3\"}";

rapidjson::Document                 JSON;
rapidjson::Value::MemberIterator    M;
const char                          *key,*value;

JSON.Parse<0>(jsonbuf);

if (JSON.HasParseError())
{
    CCLOG("Json has errors!!!");
    return;
}

for (M=JSON.MemberonBegin(); M!=JSON.MemberonEnd(); M++)
{
    key   = M->name.GetString();
    value = M->value.GetString();

    if (key!=NULL && value!=NULL)
    {
        CCLOG("%s = %s", key,value);
    }
}
Run Code Online (Sandbox Code Playgroud)