【转】NSJSONSerialization解析JSON数据
发布时间:2020-12-16 19:10:14 所属栏目:百科 来源:网络整理
导读:1 - (IBAction)touchWriteButton:( id )sender { 2 NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; 3 [dictionary setValue: @" Anthony " forKey: @" First Name " ]; 4 [dictionary setValue: @" Robbins " forKey: @" Last Name
1 - (IBAction)touchWriteButton:(id)sender { 2 NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; 3 [dictionary setValue:@"Anthony" forKey:@"First Name"]; 4 [dictionary setValue:@"Robbins" forKey:@"Last Name"]; 5 [dictionary setValue:[NSNumber numberWithUnsignedInteger:51] forKey:@"Age"]; 6 NSArray *arrayOfAnthonysChildren = [[NSArray alloc] initWithObjects:@"Anthony's Son 1",@"Anthony's Daughter 1",@"Anthony's Son 2",@"Anthony's Son 3",@"Anthony's Daughter 2",nil]; 7 [dictionary setValue:arrayOfAnthonysChildren forKey:@"children"]; 8 NSError *error = nil; 9 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error]; 10 if (error) { 11 NSLog(@"dic->%@",error); 12 } 13 [dictionary release]; 14 BOOL succeed = [jsonData writeToFile:JSON_PATH atomically:YES]; 15 if (succeed) { 16 NSLog(@"Save succeed"); 17 }else { 18 NSLog(@"Save fail"); 19 } 20 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |