JSON解析步骤:
- (NSArray *)products
{
if (_products == nil) {
//第一步:获取JSON文件的路径:
NSString *path = [[NSBundle mainBundle]
pathForResource:@"products.json" ofType:nil];
//第二步:加载JSON文件:
NSData *data = [NSData dataWithContentsOfFile:path];
//第三步:将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;
}