數據解析_JSON(初學)

 1 /*
 2     
 3     
 4     //JSON解析  系統自帶方式 --
 5     
 6     //1.獲取路勁
 7     NSString *fielPath = [[NSBundle mainBundle]pathForResource:@"Student.json" ofType:nil];
 8     
 9     //2.講該路徑下的文件(json)轉化成 二進制數據
10     NSData *data = [NSData dataWithContentsOfFile:fielPath];
11     
12     //3.查看文件是什麼類型的數據
13     
14     //參數若是是 * ,須要對象自己  ** 表明對象地址 ,向該地址中寫入數據
15     NSError *error = nil ;
16     NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
17     
18     if (error) {
19         NSLog(@"%@",error);
20     }
21     
22     //父類指針能夠指向子類對象
23 //    NSObject *idd = [[Student alloc]init];
24     
25     self.dataArray = [NSMutableArray array];
26     
27     for (NSDictionary *dic in array) {
28         
29         Student *student = [[Student alloc]initWithDictionary:dic];
30         
31         [self.dataArray addObject:student];
32     }
33     
34     
35     
36     
37  */
38     
39 /*
40     
41     //利用第三方 JSONKit文件
42     
43     NSString *fielPath = [[NSBundle mainBundle]pathForResource:@"Student.json" ofType:nil];
44     
45     //講JSON轉化爲NSData二進制數據
46     NSData *data = [NSData dataWithContentsOfFile:fielPath];
47     //利用JSONKit 進行解析
48     NSArray *array = [data objectFromJSONData];
49     
50     
51     self.dataArray = [NSMutableArray array];
52     
53     for (NSDictionary *dic in array) {
54         
55         Student *student = [[Student alloc]initWithDictionary:dic];
56         
57         [self.dataArray addObject:student];
58     }
59     
60     
61   */
相關文章
相關標籤/搜索