iOS5.0新功能之——NSJSONSerialization解析JSON数据
来源:互联网 发布:mysql最近执行语句 编辑:程序博客网 时间:2024/06/12 02:40
以下是用于保存
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 }
下面是读取方式:
1 - (IBAction)touchReadButton:(id)sender { 2 NSData *jsonData = [[NSData alloc] initWithContentsOfFile:JSON_PATH]; /* Now try to deserialize the JSON object into a dictionary */ 3 NSError *error = nil; 4 id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error]; 5 if (jsonObject != nil && error == nil){ 6 NSLog(@"Successfully deserialized..."); 7 if ([jsonObject isKindOfClass:[NSDictionary class]]){ 8 NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject; 9 NSLog(@"Dersialized JSON Dictionary = %@", deserializedDictionary);10 } else if ([jsonObject isKindOfClass:[NSArray class]]){11 NSArray *deserializedArray = (NSArray *)jsonObject;12 NSLog(@"Dersialized JSON Array = %@", deserializedArray);13 } else {14 NSLog(@"An error happened while deserializing the JSON data.");15 }16 }17 [jsonData release];18 }
0 0
- iOS5.0新功能之——NSJSONSerialization解析JSON数据
- iOS5.0新功能之——NSJSONSerialization解析JSON数据
- iOS5.0新功能之——NSJSONSerialization解析JSON数据
- iOS5.0新功能之——NSJSONSerialization解析JSON数据
- NSJSONSerialization解析JSON数据
- JSON解析之NSJSONSerialization
- 【转】NSJSONSerialization解析JSON数据
- 【转】NSJSONSerialization解析JSON数据
- iOS5自带解析类NSJSONSerialization解析json
- 用NSJSONSerialization解析NSURLSession中的json数据
- iOS之NSJSONSerialization对json解析
- NSJSONSerialization解析JSON数据 ----JSON->NSData/NSData->JSON
- iOS网络开发——JSON数据解码(使用NSJSONSerialization)
- IOS JSON解析 NSJSONSerialization
- json解析NSJSONSerialization方法
- JSON解析 ---- NSJSONSerialization介绍
- IOS学习之解析数据(NSJSONSerialization)
- iOS --- 使用NSJSONSerialization来解析JSON格式的数据
- 闲言碎语1
- SAT数学解题策略详细介绍
- 触发器(数据库)
- 关于虚函数、纯虚函数、抽象类的疑惑小记
- Android程序:安卓程序中如何操作数据
- iOS5.0新功能之——NSJSONSerialization解析JSON数据
- nginx + keepalived 实现HA 主从模式
- equal和==,List中传递值(不传引用地址)方法
- sparkSQL 集成hive异常问题解决
- KEIL工具之半主机模式
- 【Java安全技术探索之路系列:Java可扩展安全架构】之五:JCE(一):JCE架构介绍
- scu oj 4445 Right turn 2015年四川省赛J题(模拟题)
- 【iOS开发】企业版证书($299)In-House方式发布指南
- 【Java安全技术探索之路系列:Java可扩展安全架构】之六:JCE(二):JCE类和接口