JSON解析步驟:json
- (NSArray *)productsit
{io
if (_products == nil) {table
//第一步:獲取JSON文件的路徑:error
NSString *path = [[NSBundle mainBundle]數據
pathForResource:@"products.json" ofType:nil];dict
//第二步:加載JSON文件:tab
NSData *data = [NSData dataWithContentsOfFile:path];di
//第三步:將JSON數據轉爲NSArray或者NSDictionary文件
NSArray *dictArray =
[NSJSONSerialization JSONObjectWithData:data options:
NSJSONReadingMutableContainers error:nil];
//第四步:將字典轉成模型
NSMutableArray *productArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
MJProduct *p = [MJProduct productWithDict:dict];
[productArray addObject:p];
}
_products = productArray;
}
return _products;
}