本来是使用ifsteam直接读的文件,发现android不支持,后来网上搜了下发现cocos2dx自身就带有读取文件的api
我实在是太水了
#include "JsonUtils.h"
#include "cocos2d.h"
#include <fstream>
using namespace cocos2d;
Json::Value JsonUtils::getJsonFromFile(const char* fileName){
Json::Reader reader;
const char* pszFullPath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(fileName);
unsigned long size;
char *pFileContent = (char*)CCFileUtils::sharedFileUtils()->getFileData(pszFullPath, "rb", &size);
std::string jsonfile ;
jsonfile.assign(pFileContent, size);
CCAssert( size > 0, "file is open fail!");
Json::Value root;
if (!reader.parse(jsonfile, root, false )) {
CCAssert(false, "Json::Reader Parse error!");
}
return root;
}
贴个示例,也不知道jsoncpp怎么读char*,转成string给jsoncpp读了!