cocos2d-x rapidjson

    #include "json/rapidjson.h"
    #include "json/document.h"
    rapidjson::Document doc;
    ssize_t size = 0;
    unsigned char *pBytes = nullptr;
    
    std::string filePath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName);
    pBytes = cocos2d::CCFileUtils::getInstance()->getFileData(fileName, "r", &size);
    std::string contentStr((const char*)pBytes, size);
    CC_SAFE_DELETE_ARRAY(pBytes);
    doc.Parse<rapidjson::kParseDefaultFlags>(contentStr.c_str());
    
    if(doc.HasParseError())
    {
        return;
    }
    
    if(!doc.IsObject())
    {
        return;
    }
    
    const rapidjson::Value& data = doc["character"];
    if(data.IsArray())
    {
        for (int i = 0; i < data.Size(); ++i)
        {
            const rapidjson::Value& rowData = data[i];
            std::string str = rowData["name"].GetString();
            CCLOG("-----%s",str.c_str());
        }
    }
    else
    {
        CCLOG("not array");
    }

//character.json
{
    "character": [
        {
            "name": "lee"
        },
        {
            "name": "jack"
        }
    ]
}

你可能感兴趣的:(cocos2d-x)