Search code examples
c++jsoncocos2d-xrapidjson

rapidjson cocos2d-x parsing


rapidjson::Document d;
    d.Parse<0>(chatevent.chat.c_str());

    if(d.HasMember("kelimeler"))
    {
        rapidjson::Value::MemberIterator M;
        const char *key,*value;

        for (M=d.MemberBegin(); M!=d.MemberEnd(); M++)
        {
            key   = M->name.GetString();
            value = M->value.GetString();

            if (key!=NULL && value!=NULL)
            {
                log("key: %s, value: %s", key,value);
            }
        }
    }

This is the code i use to handle json data in cocos2d-x. And here is the json:

{
    "kelimeler": [{
        "harfsayisi": 10,
        "kelime": "bibnştvdaf",
        "harfler": ["t", "s", "ç", "p", "b", "c", "h", "n", "c", "c", "n", "b", "t", "v", "ş", "v", "a", "c", "v", "p", "d", "ğ", "s", "k", "i", "ç", "f", "v", "b", "p", "a", "ü", "d", "ü", "e"]
    }]
}

So how to handle it using the code? I simply can not get the "kelimeler" branch. Thanks in advance.


Solution

  • No where in you're code does it actually get the "kelimeler" array. See the rapidjson tutorial http://rapidjson.org/md_doc_tutorial.html#ValueDocument

    if(d.HasMember("kelimeler"))
    {
        const Value& k = d["kelimeler"];  // you are missing this
        assert(k.IsArray());
        for (SizeType i = 0; i < k.Size(); i++) 
        {
             ...
        }
    }