GS項目代碼閱讀筆記 —-JOSN解析部分

來源:互聯網
上載者:User

擷取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":[      ]   }]



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.