前端之家收集整理的这篇文章主要介绍了
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"
}
]
}