get array data from json file using rapidjson

蓝咒 提交于 2019-12-08 17:12:11

问题


I'm new in rapidjson. I have test.json which contains {"points": [1,2,3,4]}

and I use following code to get data of array "points"

std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("json/deluxe/treasurebag.json");

    unsigned long bufferSize = 0;

    const char* mFileData = (const char*)CCFileUtils::sharedFileUtils()->getFileData(fullPath.c_str(), "r", &bufferSize);

    std::string clearData(mFileData);
    size_t pos = clearData.rfind("}");
    clearData = clearData.substr(0, pos+1);
    document.Parse<0>(clearData.c_str());
    assert(document.HasMember("points"));

    const Value& a = document["points"]; // Using a reference for consecutive access is handy and faster.
    assert(a.IsArray());
    for (SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size_t.
        CCLOG("a[%d] = %d\n", i, a[i].GetInt());

and its result is

Cocos2d: a[0] = 1
Cocos2d: a[1] = 2
Cocos2d: a[2] = 3
Cocos2d: a[3] = 4

as expected. But now when I try to get data (get x and y) from an array like this

{"points": [{"y": -14.25,"x": -2.25},{"y": -13.25,"x": -5.75},{"y": -12.5,"x": -7.25}]}

an error occured and threw away in compiler:

//! Get the number of elements in array.
    SizeType Size() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size; }

Can anyone explain what i did wrong or something's missed? Sorry for my bad English.

Any helps would be appreciated.

Thanks.


回答1:


Using index for enumerating all array elements is correct, but I personally find it obsolete since C++11 range-for was introduced.

With C++11 you can enumerate values this way:

for(const auto& point : document["points"].GetArray()){
    CCLOG("{x=%f, y=%f}", point["x"].GetDouble(), point["y"].GetDouble());
}

You can also enumerate object's fields the same way (if you need to):

for(const auto& field : point.GetObject()) {
    field.name.GetString(); // Use field's name somehow...
    field.value.GetDouble(); // Use field's value somehow...
}



回答2:


Finally found it myself, The correct syntax would be document["points"][0]["x"].GetString()

for (SizeType i = 0; i < document["points"].Size(); i++){
    CCLOG("{x=%f, y=%f}", document["points"][i]["x"].GetDouble(), document["points"][i]["y"].GetDouble());
}

and the output is

Cocos2d: {x=-2.250000, y=-14.250000}
Cocos2d: {x=-5.750000, y=-13.250000}
Cocos2d: {x=-7.250000, y=-12.500000}

Hope it helps. :D



来源:https://stackoverflow.com/questions/26857349/get-array-data-from-json-file-using-rapidjson

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