cocos2dx 读取json及解析
2014-05-30 10:01
316 查看
ball.json 数据如下:
在cocos2dx中json的读取是用的rapidjson,包含在cocostudio工程中。所以我们要先引入#include "cocostudio/CocoStudio.h"
{ "entities": [ { "entity": { "TapOpposite": 0, "Interval": 0.95, "BallNum": 1 } }, { "entity": { "TapOpposite": 0, "Interval": 0.91, "BallNum": 2 } }, { "entity": { "TapOpposite": 0, "Interval": 0.95, "BallNum": 3 } } ] }
在cocos2dx中json的读取是用的rapidjson,包含在cocostudio工程中。所以我们要先引入#include "cocostudio/CocoStudio.h"
void GameWorld::readJson() { //json 文档 rapidjson::Document _doc; bool bRet = false; ssize_t size = 0; unsigned char *pBytes = NULL; do { pBytes = cocos2d::CCFileUtils::sharedFileUtils()->getFileData("ball.json", "r", &size); CC_BREAK_IF(pBytes == NULL || strcmp((char*)pBytes, "") == 0); std::string load_str((const char*)pBytes, size); CC_SAFE_DELETE_ARRAY(pBytes); _doc.Parse<0>(load_str.c_str()); CC_BREAK_IF(_doc.HasParseError()); //生成json文档对像 if(!_doc.IsObject()) return; //是否有此成员 if(!_doc.HasMember("entities")) return; // 通过[]取成员值,再根据需要转为array,int,double,string const rapidjson::Value &pArray = _doc["entities"]; //是否是数组 if(!pArray.IsArray()) return; for (rapidjson::SizeType i = 0; i < pArray.Size(); i++) { const rapidjson::Value &p = pArray[i]; if(p.HasMember("entity")) { const rapidjson::Value &valueEnt = p["entity"]; if(valueEnt.HasMember("TapOpposite") && valueEnt.HasMember("Interval") && valueEnt.HasMember("BallNum")) { const rapidjson::Value &tapOpposite = valueEnt["TapOpposite"]; int tapOp = tapOpposite.GetInt(); //得到int值 const rapidjson::Value &interval = valueEnt["Interval"]; float inter = interval.GetDouble(); //得到float,double值 const rapidjson::Value &ballNum = valueEnt["BallNum"]; int ball = ballNum.GetInt(); //得到int值 ballParam param; param.tapOp = tapOp; param.inter = inter; param.ballIndex = ball; m_ballParamVec.push_back(param); } } else { return; } } bRet = true; } while (0); }
相关文章推荐
- cocos2dx 解析数据之读取JSON文件
- cocos2dx 读取json及解析
- cocos2dx 读取json及解析
- cocos2dx 读取json及解析
- cocos2dx 读取rapidjson及解析
- cocos2dx 2.2.2 cocostudio 数据编辑器导出的.json文件读取 解析
- cocos2dx http json解析
- Android数据存储之JSON数据解析(读取部分)
- js读取解析JSON类型数据
- 教你用jQuery 和 Ajax 解析JSON数据。读取XHR妥妥的
- Cocos2dx封装、解析Json(cpp版)数据
- cocos2dx如何解析json
- JSON解析与读取
- go语言导入文件(固定长解析)(二) 增加读取json配置文件
- cocos2dx 2.2版本一下json解析
- cocos2dx中Http链接以及使用JsonCPP解析的简单函数
- cocos2dx读取xml详细解析
- cocos2dx lua json解析
- cocos2dx读取xml详细解析
- 动态读取JSON解析键值对