//引入的头文件和域,在3.2版本中json文件的解析已经不需要自己导入三方文件,已经自己集成到了引擎
#include "json/rapidjson.h"
#include "json/document.h"using namespace rapidjson;
//下面举一个比较有代表性的例子
{
"json":"json string",
"array":[{"int":1,"double":2.0,"bool":true,"hello":"你好"}]
}
解析过程如下
auto filePath = FileUtils::getInstance()->fullPathForFilename("ChaptersInfo.json");//填充文件路径
auto fileString = FileUtils::getInstance()->getStringFromFile(filePath);//获取文件内容字符串
log("\n %s \n",fileString.c_str());
Document document;
document.Parse<rapidjson::kParseDefaultFlags>(fileString.c_str());
if (document.HasParseError()) //打印解析错误
{
CCLOG("GetParseError %s\n",document.GetParseError());
}
if (document.IsObject() && document.HasMember("json")) {
CCLOG("%s\n", document["json"].GetString());//打印获取hello的值
}
rapidjson::Value & value = document["array"];
int i = 0;
rapidjson::Value& myItem = value[i];//这里有一点需要注意一下,value的下标不能直接填0,可以是其他数字,但是如果输入一个值是0的变量,就可以,希望知道原因的同僚指点一下....
int itemInt = myItem["int"].GetInt();
bool itemBool = myItem["bool"].GetBool();
std::string itemString = myItem["hello"].GetString();
log("itemInt = %d,itemBool = %d,itemString = %s",itemInt,itemBool,itemString.c_str());
对json文件的修改以后补充~正在做~~~