首页 > 其他分享 >cocos2d-x自代的Json库解析json(转)

cocos2d-x自代的Json库解析json(转)

时间:2022-12-15 20:31:32浏览次数:65  
标签:case name json break jsonChild Json cocos2d


之前在2.0.3版本的时候用过一个jsonCpp的库,不过后来的版本是好像加入了json库,因为cocostudio导出的界面数据和骨格动画数据用的其实就是json文件。没有看库里边是怎么解析的,在网上找了一下,找到了下边的文章说不定有用。现在手机游戏热火超天的时代快过去了,网上相关的文章是越来越少了,cocos2d-x用的是C++所以学习成本相对来说也比Unity3d要高很多。但这个引擎是开源的,可以使用大量的第三方C++库。这一点是没有什么游戏引擎可以比的了的。


json研究了一天,累死了,看了网上发的各种解析方法,都弄的一知半解,也不提供一个demo让我们这些菜鸟看一下,无奈,自己整一个,希望能帮到那些在json解析上卡壳的程序猿!
要解析json先得知道json是什么,什么样的结构,英文好的去​​​http://www.json.org/​​​看,官网,清晰明了。英文不好的去​​http://www.w3school.com.cn/json/json_intro.asp​​​看,讲得也不错,json本身简单,看看就可以了!
在提供一个json的矫正工具,防止在json格式上纠结:​​​http://www.ij2ee.com/bejson/bejson.html​​​好了,该上硬货了:
先导入Json.h头文件,我用的是2.2版本的,3.0版本的需要自己指一下路径,然后json获取的方法名有些变动,不过变动不是很大!
3.2版本 #include "editor-support/spine/Json.h"
void FirstLayer::parseJsonObjects(char *pJsonStr){
    Json* jsonRoot = Json_create(pJsonStr);
   
    Json* jsonChild = jsonRoot->child;
   
    //    servers = new Vector();
  
    while (jsonChild) {
//下面列举json的6种格式,都比较简单,我就用常用的string类型和比较复杂的array类型做示范吧!
        switch (jsonChild->type) {
            case Json_NULL:{
               
                break;
            }
            case Json_Number:{
           
                break;
            }
            case Json_Object:{
                break;
            }
            case Json_String:{
                CCLOG("<<<<<<<<%s",jsonChild->valuestring);
                break;
            }
            case Json_True:{
                break;
            }
            case Json_Array:{
                Json *json=jsonChild->child;
                CCLOG("size=%d",Json_getSize(jsonChild));
//在这我给出三种遍历json数组的方法,让不同编程风格的人都能找到自己喜欢的遍历方式
//第一种:
               
               
//第二种
                for (int i=0; i
                    Json *jjson=Json_getItemAt(jsonChild, i);
                    CCLOG("<>name=%s",Json_getItem(jjson, "name")->valuestring);
                }
//第三种
               
                while (json) {
                    //单独遍历法
                   
//                    CCLOG("firstName=%s,lastName=%s",Json_getString(json, "firstName", NULL),Json_getString(json, "lastName", ""));
                    Json* tags = Json_getItem(json,"tags");
                    Json* name = Json_getItem(json,"name");
                    Json* source = Json_getItem(json,"source");
                    CCLOG("tags:%s,name=%s,source=%s", tags->valuestring,name->valuestring,source->valuestring);
                    json=json->next;
                }
                break;
            }
            default:
                break;
        }
        jsonChild=jsonChild->next;
    }

   //遍历结束,释放json对象结构
    Json_dispose(jsonRoot);
}

在需要调用上述方法的地方写上如下代码:
//获取json文件的路径
std::string path=CCFileUtils::sharedFileUtils()->fullPathForFilename("newFile.json");
    unsigned long len=0;
    if(CCFileUtils::sharedFileUtils()->isFileExist(path)){
//读取文件数据
    unsigned char* str=CCFileUtils::sharedFileUtils()->getFileData("newFile.json", "r", &len);
//调用上面写的解析函数
    this->parseJsonObjects((char*)str);
    }
OK,是不是就成功了!
好了,是不是很简单,这是cocos2dx中自带的方法,还有大神介绍libjson和jsonbox来解析,我会继续关注的,大家感兴趣的也可以去研究一下,希望能写出详细的教程,让我这等菜鸟能够快速入门吧!

标签:case,name,json,break,jsonChild,Json,cocos2d
From: https://blog.51cto.com/u_13760719/5946044

相关文章