Cocos2d 怎么使用json? Cocos2d 使用json 解析用 cocos/editot-support/cocosstu
dio/DicitionaryHelper.h 首先介绍json 如下: { // json 起始位置 // type : value "classname": null,"name": null,"animation": { // sub json,可以被认为是json "classname": null,"name": "AnimationManager","actionlist": [] // sub json 数组,此处为0 },"dataScale": 1,"designHeight": 320,"designWidth": 480,"textures": [],"version": "1.2.0.1","widgetTree": { "classname": "Panel","children": [ { "classname": "Panel","children": [ // sub json 数组,此处解析,需要先
获取其size,再遍历sub json { // sub json "classname": "Panel","children": [],"options": { "__type": "ComGUIPanelSurrogate:#EditorCommon.JsonModel.Component.GUI","classname": "Panel","name": "Panel","ZOrder": 1,"actiontag": 109,"anchorPointX": 0,"anchorPointY": 0,"classType": null,"colorB": 255,"colorG": 255,"colorR": 255,"flipX": false,"flipY": false,"height": 57,"ignoreSize": false,"layoutParameter": { "classname": null,"align": 2,"gravity": 0,"layoutEageType": 0,"layoutNormalHorizontal": 0,"layoutNormalVertical": 0,"layoutParentHorizontal": 1,"layoutParentVertical": 0,"marginDown": 0,"marginLeft": 0,"marginRight": 0,"marginTop": 0,"relativeName": "Panel","relativeToName": "root_Panel","type": 2 },"opacity": 255,"positionPercentX": 0,"positionPercentY": 0.821875,"positionType": 0,"rotation": 0,"scaleX": 1,"scaleY": 1,"sizePercentX": 1,"sizePercentY": 0.178125,"sizeType": 0,"tag": 82,"touchAble": false,"useMergedTexture": false,"visible": true,"width": 480,"x": 0,"y": 263,"backGroundImage": null,"backGroundImageData": { "path": "ribbon.png","plistFile": null,"resourceType": 0 },"backGroundScale9Enable": true,"bgColorB": 255,"bgColorG": 200,"bgColorOpacity": 100,"bgColorR": 150,"bgEndColorB": 255,"bgEndColorG": 0,"bgEndColorR": 0,"bgStartColorB": 255,"bgStartColorG": 255,"bgStartColorR": 255,"capInsetsHeight": 66.6666641,"capInsetsWidth": 66.6666641,"capInsetsX": 66.6666641,"capInsetsY": 66.6666641,"clipAble": false,"colorType": 0,"layoutType": 0,"vectorX": 0,"vectorY": -0.5 } },{ "classname": "Label","options": { "__type": "ComGUILabelSurrogate:#EditorCommon.JsonModel.Component.GUI","classname": "Label","name": "UItest","actiontag": 115,"anchorPointX": 0.5,"anchorPointY": 0.5,"height": 27,"ignoreSize": true,"relativeName": "UItest","positionPercentX": 0.5,"positionPercentY": 0.96875,"sizePercentX": 0.197916672,"sizePercentY": 0.084375,"tag": 88,"width": 95,"x": 240,"y": 310,"areaHeight": 0,"areaWidth": 0,"fontFile": null,"fontName": "宋体","fontSize": 20,"hAlignment": 0,"text": "UI_test","touchScaleEnable": false,"vAlignment": 0 } },cocos2d json 解析应用如下: const char *fileName = "abc.json"; std::string jsonpath; rapidjson::Document jsonDict; jsonpath = fileName; // get data from file name std::string contentStr = FileUtils::getInstance()->getStringFromFile(jsonpath); // parse data to json jsonDict.Parse<0>(contentStr.c_str()); if (jsonDict.HasParseError()) { CCLOG("GetParseError %s\n",jsonDict.GetParseError()); } // get "version" value from json const char* fileVersion = DICTOOL->getStringValue_json(jsonDict,"version"); // get sub json array // 1. get sub json array size int texturesCount = DICTOOL->getArrayCount_json(data,"textures"); for (int i=0; i<texturesCount; i++) { // 2. get sub json const char* file = DICTOOL->getStringValueFromArray_json(data,"textures",i); } // get sub json const rapidjson::Value& widgetTree = DICTOOL->getSubDictionary_json(data,"widgetTree");