gokturk
gokturk

Reputation: 116

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.

Upvotes: 0

Views: 1959

Answers (2)

Steve T
Steve T

Reputation: 7838

Need to remember to watch out for namespace collisions. Ideally the compiler warns about ambiguity. In your case you need to specify you want the classes from rapidjson and not cocos2d.

Updating Josh's example.

rapidjson::Document d;
d.Parse<0>(chatevent.chat.c_str());
if(d.HasMember("kelimeler"))
{
    const rapidjson::Value& k = d["kelimeler"];  // you are missing this
    assert(k.IsArray());
    if(k.HasMember("harfler")) 
    {
        const rapidjson::Value& h = k["harfler"];
        for (rapidjson::SizeType i = 0; i < h.Size(); i++) 
        {
             log("value: %s", h[i].GetString());
        }
    }
}

Upvotes: 1

Josh
Josh

Reputation: 73

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++) 
    {
         ...
    }
}

Upvotes: 0

Related Questions