当前位置:编程学习 > wap >>

CocoStudioGameDataEditor数据读取方法学习

data.xlsc截图


经过CocostudioGameDataEditor导出之后,生成data_Property.json,截图如下:



程序读取方法,如下代码 方法1:

//json数据读取方法1
unsigned long  len = 0; 
unsigned char*  des = cocos2d::CCFileUtils::sharedFileUtils()->getFileData("data_Property.json", "r", &len);  
CSJson::Reader  reader; 
CSJson::Value  jsValue; 
const std::string strTemp((char*)des); 

bool parseRet = reader.parse(strTemp,jsValue,false); 

unsigned int nCount = jsValue.size(); 
for (unsigned int i=0; i < nCount; ++i)  

if( jsValue[i]["Name"].empty() ) //有效键值检测

continue; 
}

std::string strName  = jsValue[i]["Name"].asString(); 
int ilevel  = jsValue[i]["Level"].asInt();   




将data_Property.json修改下,得到data.json (截图如下),



可以通过如下代码 方法2 读取其中数据;

//json数据读取方法2
// 初始化字典 
unsigned long len = 0; 
unsigned char* des = cocos2d::CCFileUtils::sharedFileUtils()->getFileData("data.json", "r", &len); 
cs::CSJsonDictionary *dict = new cs::CSJsonDictionary(); 
dict->initWithDescription((const char*)des); 
// 读取数据 
//cs::CSJsonDictionary* sub = dict->getSubObjectFromArrayByIndex(0);//该方法还未释放,请等待11月19日版本。 

char * pKey = "data";
unsigned int uCount = dict->getArrayItemCount(pKey);
for (unsigned int i = 0; i < uCount; ++i)
{
    cs::CSJsonDictionary *sub = dict->getSubItemFromArray(pKey, i);
    std::string name = sub->getItemStringValue("Name"); 
    int diff = sub->getItemIntValue("Difficulty ", -1); 
}



PS:
方法2代码中,

//cs::CSJsonDictionary* sub = dict->getSubObjectFromArrayByIndex(0);//该方法还未释放,请等待11月19日版本。  

很可惜,19号更新的cocos2dx v2.2.1 并没有释放这个方法……

补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,