iOS开发: 网络解析数据之JSON解析
NSMutableArray *jsonArray = [NSMutableArray array];
//获取路径:NSBundle会自动获取路径(首先要在项目中导入易车.txt文件)
NSString *path = [[NSBundle mainBundle] pathForResource:@"易车" ofType:@"txt"];
*说明:@“易车”是文件名, @“txt”是文件类型。
NSData *data = [NSData dataWithContentsOfFile:path];
//JSON解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//解析完成后可以使用需要的数组或字典。
NSArray *array = [[dic objectForKey:@"data"] objectForKey:@"list"];
for (NSDictionary *dataDic in array) {
YiCar *yicar = [[[YiCar alloc]init]autorelease];
[yicar setValuesForKeysWithDictionary:dataDic];
[jsonArray addObject:yicar];
}
}