您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x把json数据解析到数组或字典中(libjson库)

2013-09-17 23:30 531 查看
  以前在cocos2d-x项目中用到json解析,集成了libjson库后发现网上提供的解析方法大多是在解析过程中取得值,并没有将解析结果有效的保存起来,于是摸索一番,把解析结果根据数据格式存到数组或字典当中。

  不敢独享,代码奉上:

void ccout(CCObject *thob,int donotset=0,bool dot=false);

void AppDelegate::ccout(CCObject *thob,int donotset,bool dot){
if (dynamic_cast<CCDictionary *>(thob)) {
cout<<"{";
CCDictionary *temp=(CCDictionary *)thob;
CCDictElement *ele;
CCDICT_FOREACH(temp, ele){
const char *key=ele->getStrKey();
CCObject *ob=ele->getObject();
cout<<endl;
for (int i=0; i<donotset+1; i++)cout<<"\t";
cout<<key<<"=";
this->ccout(ob,donotset+2);
cout<<";";
}
cout<<std::endl;
for (int i=0; i<donotset; i++)cout<<"\t";
cout<<"}";
if (dot) {
cout<<",";
}
}else if (dynamic_cast<CCArray *>(thob)){
cout<<"(";
CCArray *temp=(CCArray *)thob;
CCObject *ob;
CCARRAY_FOREACH(temp, ob){
if (dynamic_cast<CCDictionary *>(ob)) {
if (ob!=temp->lastObject()) {
this->ccout(ob,donotset+1,true);
}else{
this->ccout(ob,donotset+1);
}
}else
if (dynamic_cast<CCArray *>(ob)) {
this->ccout(ob,donotset+1);
}else if (dynamic_cast<CCString *>(ob)) {
cout<<((CCString*)ob)->getCString();
if (ob!=temp->lastObject()) {
cout<<",";
}
}else{
cout<<"undefined class cout";
}

}
cout<<")";
}

}


View Code
  输出结果:

({
中国食谱=(上海醉蟹,北京锅烧鸡,川式凉拌豇豆,清真酱牛肉);
国外食谱=(泰式柠檬肉片,鸡柳汉堡,蒸桂鱼卷 );
更多={
中式=(南,北,{
地方=(小吃,大餐);
},更多选择);
};
},{
菜谱分类=(上海菜,贵州菜,潮汕菜,云南菜,东北菜,安徽菜,广东菜,浙江菜,湖南菜);
},其它)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐