cocos2d 3.3 解析json

Cocos2d 怎么使用json?

Cocos2d 使用json 解析用
cocos/editot-support/cocosstudio/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",
    "name": null,
    "children": [
      {
        "classname": "Panel",
        "name": null,
        "children": [ // sub json 数组,此处解析,需要先获取其size,再遍历sub json
          { // sub json
            "classname": "Panel",
            "name": null,
            "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,
                "name": 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",
            "name": null,
            "children": [],
            "options": {
              "__type": "ComGUILabelSurrogate:#EditorCommon.JsonModel.Component.GUI",
              "classname": "Label",
              "name": "UItest",
              "ZOrder": 1,
              "actiontag": 115,
              "anchorPointX": 0.5,
              "anchorPointY": 0.5,
              "classType": null,
              "colorB": 255,
              "colorG": 255,
              "colorR": 255,
              "flipX": false,
              "flipY": false,
              "height": 27,
              "ignoreSize": true,
              "layoutParameter": {
                "classname": null,
                "name": null,
                "align": 2,
                "gravity": 0,
                "layoutEageType": 0,
                "layoutNormalHorizontal": 0,
                "layoutNormalVertical": 0,
                "layoutParentHorizontal": 1,
                "layoutParentVertical": 0,
                "marginDown": 0,
                "marginLeft": 0,
                "marginRight": 0,
                "marginTop": 0,
                "relativeName": "UItest",
                "relativeToName": "root_Panel",
                "type": 2
              },
              "opacity": 255,
              "positionPercentX": 0.5,
              "positionPercentY": 0.96875,
              "positionType": 0,
              "rotation": 0,
              "scaleX": 1,
              "scaleY": 1,
              "sizePercentX": 0.197916672,
              "sizePercentY": 0.084375,
              "sizeType": 0,
              "tag": 88,
              "touchAble": false,
              "useMergedTexture": false,
              "visible": true,
              "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");




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值