1.我们准备了一个json文件(test.json),在resource下面:
{ "data": { "lastArea": { "areaId": 200,"label": 1,"name": "我是区服","state": 1 },"topArea": { "areaId": 10,"label": 0,"total": 2 },"rc": 0 }
2.我们在onEnter函数中加入如下代码
const char *des = NULL; std::string jsonpath; cs::CSJsonDictionary *jsonDict = NULL; jsonpath = CCFileUtils::sharedFileUtils()->fullPathForFilename("test.json"); unsigned long size = 0; des = (char*)(CCFileUtils::sharedFileUtils()->getFileData(jsonpath.c_str(),"r",&size)); if(NULL == des || strcmp(des,"") == 0) { printf("read json file[%s] error!\n","test.json"); } CSJson::Reader cReader; CSJson::Value value; if(!cReader.parse(std::string(des),value,false)) {CCLog("%s",cReader.getFormattedErrorMessages().data());}这样,我们的value对象中就存储到了json里面的数据。我们则可以取里面的数据。取法如下
CCLog("areadId:%d,label:%d,name:%s,state:%d",value["data"]["lastArea"]["areaId"].asInt(),value["data"]["lastArea"]["label"].asInt(),value["data"]["lastArea"]["name"].asCString(),value["data"]["lastArea"]["state"].asInt());