iOS开发之JSON解析

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;

}

相关文章
相关标签/搜索