JSON->NSData
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 }
本文转自编程小翁博客园博客,原文链接:http://www.cnblogs.com/wengzilin/archive/2012/07/17/2595683.html,如需转载请自行联系原作者