解析数据的步骤json
一、plist文件数据数组
//获取文件路径对象
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];ci
//从文件路径中提取数组it
NSArray *array = [NSArray arrayWithContentsOfFile:filePath];io
//初始化数据数组table
_dataArray = [[NSMutableArray alloc] initWithCapacity:0];file
//遍历数组,进行添加模型model
for (NSDictionary *dic in array) {遍历
Student *student = [[Student alloc] init];
[student setValuesForKeysWithDictionary:dic];
[_dataArray addObject:student];
[student release];
}
二、解析JSON数据
//获取json数据的路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"json"];
//获取NSData对象
NSData *data = [NSData dataWithContentsOfFile:filePath];
//解析JSON数据
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//初始化模型数组
_dataArray = [[NSMutableArray alloc] initWithCapacity:0];
for (NSDictionary *dic in array) {
Model *model = [[Model alloc] init];
[model setValuesForKeysWithDictionary:dic];
[_dataArray addObject:model];
[model release];
}
从上面两个例子能够看出来,解析数据的步骤大概能够分为如下几步:
一、获取数据的路径
二、从文件路径中提取对应的数据类型
三、解析数据
(1)初始化模型数组
(2)解析的最终结果都是将字典转换成模型,因此咱们要理清层次关系,明白字典是有键—值对组成的。
(3)将模型加到事先声明的字典或者数组中。