Objective-C JSON操作
2015-08-27 13:34
507 查看
Objective-C 操作JSON 主要使用的是 NSJSONSerialization 这个类
NSJSONSerialization 包含了以下五个类函数判断 该实例(obj)是否为JSONObject
需满足下面三个条件
1.obj 是NSArray 或 NSDictionay 以及他们派生出来的子类
2.obj 包含的所有对象是NSString,NSNumber,NSArray,NSDictionary 或NSNull
3.NSNumber的对象不能是NaN或无穷大
<span style="font-size:18px;">+ (BOOL)isValidJSONObject:(id)obj;</span>
将JSON数据写为NSData数据,其中opt参数的枚举如下,这个参数可以设置,也可以不设置,如果设置,则会输出视觉美观的JSON数据,否则输出紧凑的JSON数据。
<span style="font-size:18px;">+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;</span>
这个方法是解析中数据的核心方法,data是JSON数据对象,可以设置一个opt参数,具体用法如下:
<span style="font-size:18px;">typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) { //将解析的数组和字典设置为可变对象 NSJSONReadingMutableContainers = (1UL << 0), //将解析数据的子节点创建为可变字符串对象 NSJSONReadingMutableLeaves = (1UL << 1), //允许解析对象的最上层不是字典或者数组 NSJSONReadingAllowFragments = (1UL << 2) }</span>
<span style="font-size:18px;">+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;</span>
将JSON数据写入到输出流,返回的是写入流的字节数
<span style="font-size:18px;">+ (NSInteger)writeJSONObject:(id)obj toStream:(NSOutputStream *)stream options:(NSJSONWritingOptions)opt error:(NSError **)error;</span>
从输入流读取JSON数据
<span style="font-size:18px;">+ (id)JSONObjectWithStream:(NSInputStream *)stream options:(NSJSONReadingOptions)opt error:(NSError **)error;</span>
<span style="font-size:18px;">NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init]; [dictionary setValue:@"xiaominfc" forKey:@"username"]; [dictionary setValue:@"1991-03-26" forKey:@"birthday"]; [dictionary setValue:[NSNumber numberWithInteger:23] forKey:@"age"]; NSArray *arrayOfAnthonysChildren = [[NSArray alloc]initWithObjects:@"Java",@"Objective-C",@"Python",@"C++", nil]; [dictionary setValue:arrayOfAnthonysChildren forKey:@"program_language"]; if([NSJSONSerialization isValidJSONObject:dictionary]){ NSLog(@"it is a JSONObject!"); } //use dataWithJSONObject fun NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error]; if([jsonData length] > 0 && error == nil) { NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"data:%@",jsonString); } //use JSONObjectWithData fun NSString *jsonDataString = @"{\"username\":\"xiaominfc\",\"city\":\"深圳\"}"; NSData *data = [jsonDataString dataUsingEncoding:NSUTF8StringEncoding]; id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; if ([jsonObject isKindOfClass:[NSDictionary class]]) { NSDictionary *jsonDictionary = (NSDictionary*)jsonObject; NSLog(@"username:%@ And city:%@",[jsonDictionary valueForKey:@"username"],[jsonDictionary valueForKey:@"city"]); } //use writeJSONObject fun NSString *filePath = @"/Users/xiaominfc/text.txt"; NSOutputStream *outStream = [[NSOutputStream alloc]initToFileAtPath:filePath append:NO]; [outStream open]; NSInteger length = [NSJSONSerialization writeJSONObject:dictionary toStream:outStream options:NSJSONWritingPrettyPrinted error:&error]; NSLog(@"write %ld bytes",(long)length); [outStream close]; //use JSONObjectWithStream NSInputStream *inStream = [[NSInputStream alloc]initWithFileAtPath:filePath]; [inStream open]; id streamObject = [NSJSONSerialization JSONObjectWithStream:inStream options:NSJSONReadingAllowFragments error:&error]; if ([streamObject isKindOfClass:[NSDictionary class]]) { NSDictionary *jsonDictionary = (NSDictionary*)streamObject; NSNumber *ageNumber = (NSNumber*)[jsonDictionary valueForKey:@"age"]; NSLog(@"username:%@ And age:%d",[jsonDictionary valueForKey:@"username"],[ageNumber intValue]); } [inStream close];</span>
相关文章推荐
- Java____java.lang.object
- OC基础回顾(十四)文件加载与保存
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法
- objective-c 方法加号(+) 减号(-)
- Java IO流->处理流->对象流:ObjectOutputStream&ObjectInputStream
- DSO Framer Control Object 操作word文件
- 详谈OC(object-c)深浅复制/拷贝-什么情况下用retain和copy
- Object -C NSValue -- 笔记
- 【读书笔记】iOS-Objective-C对C的扩展基础知识
- 【读书笔记】iOS-Objective-C对C的扩展基础知识
- Objective-C 【构造方法(重写、场景、自定义)、super】
- Objective-C 【构造方法(重写、场景、自定义)、super】
- 关于objectIsForcedForKey这个方法不好用的原因
- Object -C NSSet -- 笔记
- ObjectiveC开发教程--如何去除字符串中的空格和回车
- 将Map<String,Object> 转String存储数据库
- Objective-C 中 +load 与 +initialize
- 斯坦福大学iOS应用开发教程学习笔记(第一课) MVC/Objective-C
- 黑马程序员——ios基础---Objective-C:ARC、类别、Block、协议
- Objective-C( 语法二)