解析数据的步骤
解析數據的步驟
1、plist文件數據
? ? //獲取文件路徑
? ? NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];
?? ?
? ? //從文件路徑中提取數組
? ? NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
?? ?
? ? //初始化數據數組
? ? _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
?? ?
? ? //遍歷數組,進行添加模型
? ? for (NSDictionary *dic in array) {
? ? ? ? Student *student = [[Student alloc] init];
? ? ? ? [student setValuesForKeysWithDictionary:dic];
? ? ? ? [_dataArray addObject:student];
? ? ? ? [student release];
? ? }
?
?
2、解析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、解析數據
(1)初始化模型數組
(2)解析的最終結果都是將字典轉換成模型,所以我們要理清層次關系,明白字典是有鍵—值對組成的。
(3)將模型加到事先聲明的字典或者數組中。
轉載于:https://www.cnblogs.com/d-mm/p/5210222.html
總結
- 上一篇: 视觉SLAM十四讲 罗德里格斯公式推导
- 下一篇: hdu4405:概率dp