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