GS项目代码阅读笔记 ----JOSN解析部分

获取app所在路径 设置到JSonViewController中去

根据路径找到bundle文件

找到bundle文件中横屏和竖屏的JSON文件 : viewStruct_landScape.json  viewStruct_portrait.json


解析JSON文件:[self constructViewWithJsonPath:jsonpath];

读取JSON文件中所有的数据

NSString * json = [NSString  stringWithContentsOfFile:jsonpath encoding:NSUTF8StringEncoding error:&error];

用SBJsonParser解析:

SBJsonParser * jsonParser = [SBJsonParser new];

        id repr = [jsonParser objectWithString:json];

        if (!repr){

NSLog(@"-JSONValue failed. Error trace is: %@", [jsonParser errorTrace]);

            [jsonParser release];}


所有数据都在repr中

repr是个容器 可能是Dictionary 或者 Array

所以要判断repr的类型:

if ([item isKindOfClass:[NSDictionary class]])

       // 解析

}

else if ([item isKindOfClass:[NSArray class]])

       // 解析

}


进行解析:

取出 class ,frame,propertyList,content,subViews五个类型

class :判断是否以结尾Controller,也就是判断是否这个类是个UIViewController,还是UIView,如果是Controller,则。。。。。。

frame

propertyList:这个结构比较复杂,里面可能会嵌套了N个{ class ,frame,propertyList,content,subViews}

content

subViews

class中为什么要区分是UIView还是UIViewController呢?因为后面需要对这个类进行初始化,这两个类需要的初始化信息是不一样的。

在解析这两个类的时候,也要区分是UIView还是UIViewController,因为这两个不同的class决定了下面解析的属性,各自按各自的规则去解析属性



[
   {
      "class":"SalmonFlipView",
      "frame":"{{0, 0}, {768, 1024}}",
      "propertyList":{
         "frontView":{
            "class":"SalmonFlipViewSubViewWithButton",
            "frame":"{{0, 0}, {768,1024}}",
            "propertyList":{
               "flipViewButton":{
                  "class":"SalmonButton",
                  "frame":"{{134, 437}, {512, 512}}",
                  "propertyList":{
                     "backgroundColor":{
                        "UIColor":{
                           "r":0,
                           "g":0,
                           "b":255,
                           "a":1
                        }
                     },
                     "animations":{
                        "animation":[
                           {
                              "class":"CABasicAnimation",
                              "propertyList":{
                                 "fromValue":{
                                    "float":"0"
                                 },
                                 "toValue":{
                                    "float":"1"
                                 },
                                 "keyPath":{
                                    "string":"opacity"
                                 },
                                 "duration":{
                                    "float":"4"
                                 }
                              }
                           }
                        ]
                     }
                  },
                  "subViews":[

                  ],
                  "content":""
               }
            },
            "subViews":[
               {
                  "class":"UIImageView",
                  "frame":"{{0, 0}, {461, 593}}",
                  "propertyList":{
                     "backgroundColor":{
                        "UIColor":{
                           "r":51,
                           "g":51,
                           "b":0,
                           "a":1
                        }
                     },
                     "animations":{
                        "animation":[
                           {
                              "class":"CABasicAnimation",
                              "propertyList":{
                                 "fromValue":{
                                    "rect":"{{100, 100}, {0, 0}}"
                                 },
                                 "toValue":{
                                    "rect":"{{0, 0}, {461, 593}}"
                                 },
                                 "keyPath":{
                                    "string":"frame"
                                 },
                                 "duration":{
                                    "float":"2"
                                 }
                              }
                           }
                        ]
                     }
                  },
                  "subViews":[

                  ],
                  "content":""
               }
            ]
         },
         "backgroundView":{
            "class":"SalmonFlipMoviePlayerView",
            "frame":"{{107, 173}, {541, 572}}",
            "propertyList":{
               "contentURL":{
                  "URL":"http:\/\/manager.mediapad.cn\/26\/page_1220.bundle\/1.mp4"
               },
               "sandBoxContentURL":{
                  "URL":"http:\/\/manager.mediapad\/1\/26\/page_1220.bundle\/1.mp4"
               },
               "closeButton":{
                  "class":"SalmonButton",
                  "frame":"{{497, 0}, {44, 44}}",
                  "propertyList":[

                  ],
                  "subViews":[

                  ],
                  "content":"moviePlayerCloseButton.png"
               }
            },
            "subViews":[

            ]
         }
      },
      "subViews":[

      ]
   }
]





你可能感兴趣的:(json,Class,animation,UIView,float,Dictionary)