get-array-Daten aus der json-Datei mit rapidjson

Ich bin neu in rapidjson. Ich habe test.json enthält {"points": [1,2,3,4]}

und ich verwende folgenden code, um Daten von 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());

und das Ergebnis ist

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

als erwartet. Aber wenn ich jetzt versuche die Daten (get x und y) aus einem array wie diesem

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

ist ein Fehler aufgetreten und warf im compiler:

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

Kann mir jemand erklären, was ich falsch gemacht habe oder etwas übersehen? Sorry für mein schlechtes Englisch.

Jeder hilft gerne.

Dank.

  • Die zweite json fehlt die geschweifte Klammer am Ende.
InformationsquelleAutor Nikel Arteta | 2014-11-11
Schreibe einen Kommentar