How to get array inside json into vector?

Example 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"]
    }]
}

Using rapidjson to handle json. But i need to get array under “kelimeler” branch and then handle jsons inside that array with rapid json. So, how to get the array in cocos2d-x?

Here’s an example on S.O.

rapidjson::Document document;
if (document.Parse<0>(json).HasParseError() == false) {
    const Value& a = document["first"];
    const Value& b = a["next"];
    // rapidjson uses SizeType instead of size_t.
    for (rapidjson::SizeType i = 0; i < b.Size(); i++) {
        const Value& c = b[i];
        printf("%s \n",c["key"].GetString());
    }
}

You can also look at my reply for details on handling this by converting it to a ValueMap if you’re more familiar with that.

Hi. Thank you for your reply. I tried it before, tried again but i keep getting the error:

jni/../../Classes/GameScene.cpp: In member function 'virtual void GameScene::onChatReceived(AppWarp::chat)':
jni/../../Classes/GameScene.cpp:177:36: error: invalid initialization of reference of type 'const cocos2d::Value&' from expression of type 'rapidjson::GenericValue<rapidjson::UTF8<> >'
      const Value& a = d["kelimeler"];
                                    ^
jni/../../Classes/GameScene.cpp:180:44: error: 'const class cocos2d::Value' has no member named 'Size'
      for (rapidjson::SizeType i = 0; i < a.Size(); i++) {
                                            ^
jni/../../Classes/GameScene.cpp:181:28: error: no match for 'operator[]' (operand types are 'const cocos2d::Value' and 'rapidjson::SizeType {aka unsigned int}')
          const Value& c = a[i];
                            ^
jni/../../Classes/GameScene.cpp:182:26: error: no match for 'operator[]' (operand types are 'const cocos2d::Value' and 'const char [4]')
          printf("%s \n",c["key"].GetString());
                          ^

You need to specify which Value you want to use since cocos2d also has a class with that name.
rapidjson::Value instead of Value. You could also try using rapidjson::Value in your cpp file instead.

1 Like

By the way, is there a possible way to handle rapidjson array into cocos2d x vector?

We handle json in our Networking video, perhaps it will be of some help https://github.com/SonarSystems/Cocos2d-x-Networking/blob/master/C%2B%2B%20Examples/Return%20JSON%20Array%20From%20Server%20To%20Cocos2d-x/Classes/HelloWorldScene.cpp

Thank you for your reply. Isn’t there a way to assign rapidjson data into a cocos2d vector?

@SonarSystems how do you get “harfler” array into a cocos2dx vector or array or something with your example?

What is harfler?

It’s the name of an array in their data. See the first post.

"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"]
    }]
1 Like

Get the entire array and access that particular index.

Yes there is, and I already posted a link :smiley: But here it is again for ease of finding it. The code should compile and work to convert a JSON string into a cocos2d::ValueMap or cocos2d::ValueVector.

Edit: correct typo

Im kind of confused. It is cocos2d::ValueArray?

Sorry, typo. cocos2d::ValueVector.

Here is my own solution:

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

if(d.HasMember("kelimeler") == true)
{
    const rapidjson::Value& a = d["kelimeler"];
    for (rapidjson::SizeType i = 0; i < a.Size(); i++)
    {
        const rapidjson::Value& c = a[i];
        const rapidjson::Value& harfler = c["harfler"];
        for (rapidjson::SizeType i = 0; i < harfler.Size(); i++)
        {
            this->arr[i] = harfler[i].GetString();
        }
    }
}

arr is: std::string arr[35]; in .h file.