iOS5.0新功能之——NSJSONSerialization解析JSON数据
来源:互联网 发布:linux密码忘了 编辑:程序博客网 时间:2024/06/01 09:49
以下是用于保存
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格式的数据
- Spring与JMX集成
- 在eclipse下使用java调用weka【第一步】
- ThinkPad T440s(20AQS01100)
- iOS NSString 和NSData 转换
- 第七章 查找 知识导图
- iOS5.0新功能之——NSJSONSerialization解析JSON数据
- C++ 函数指针的详解
- Java线程:线程状态的转换
- 回溯法+奇偶剪枝——Hdu 1010 Tempter of the Bone
- Cocos2dx多点触摸实例
- 第一篇博客
- MySQL优化—工欲善其事,必先利其器之EXPLAIN
- First Step
- csharp: using OleDb Getting the identity of the most recently added record