Objective-C JSON操作
2015-11-13 09:14
375 查看
Objective-C
操作JSON 主要使用的是 NSJSONSerialization
这个类
NSJSONSerialization
包含了以下五个类函数
+
(BOOL)isValidJSONObject:(id)obj;
判断
该实例(obj)是否为JSONObject
需满足下面三个条件
1.obj
是NSArray 或 NSDictionay 以及他们派生出来的子类
2.obj
包含的所有对象是NSString,NSNumber,NSArray,NSDictionary 或NSNull
3.NSNumber的对象不能是NaN或无穷大
+
(NSData *)dataWithJSONObject:(id)obj
options:(NSJSONWritingOptions)opt error:(NSError
**)error;
将JSONObject的实例转成NSData
+
(id)JSONObjectWithData:(NSData *)data
options:(NSJSONReadingOptions)opt error:(NSError
**)error;
将NSData类型的实例转成JSONObject
+
(NSInteger)writeJSONObject:(id)obj toStream:(NSOutputStream
*)stream options:(NSJSONWritingOptions)opt error:(NSError
**)error;
将一个JSONObject的实例写入到一个输出流中
返回写入的长度
+
(id)JSONObjectWithStream:(NSInputStream *)stream
options:(NSJSONReadingOptions)opt error:(NSError
**)error;
从输入流中读取成JSONObject
并返回
代码演示:
Objective-c代码
JSON操作" />
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];
操作JSON 主要使用的是 NSJSONSerialization
这个类
NSJSONSerialization
包含了以下五个类函数
+
(BOOL)isValidJSONObject:(id)obj;
判断
该实例(obj)是否为JSONObject
需满足下面三个条件
1.obj
是NSArray 或 NSDictionay 以及他们派生出来的子类
2.obj
包含的所有对象是NSString,NSNumber,NSArray,NSDictionary 或NSNull
3.NSNumber的对象不能是NaN或无穷大
+
(NSData *)dataWithJSONObject:(id)obj
options:(NSJSONWritingOptions)opt error:(NSError
**)error;
将JSONObject的实例转成NSData
+
(id)JSONObjectWithData:(NSData *)data
options:(NSJSONReadingOptions)opt error:(NSError
**)error;
将NSData类型的实例转成JSONObject
+
(NSInteger)writeJSONObject:(id)obj toStream:(NSOutputStream
*)stream options:(NSJSONWritingOptions)opt error:(NSError
**)error;
将一个JSONObject的实例写入到一个输出流中
返回写入的长度
+
(id)JSONObjectWithStream:(NSInputStream *)stream
options:(NSJSONReadingOptions)opt error:(NSError
**)error;
从输入流中读取成JSONObject
并返回
代码演示:
Objective-c代码
JSON操作" />
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];
相关文章推荐
- OC中的小知识点
- Objective-C中将枚举转换成NSString的一种思路
- Objective-c:数据类型
- OC中的成员变量
- Objective-c:封装、继承与多态
- OBjectve-c 基本数据类型 总结
- objective-c UIImageView
- Objective-c 手写UIDatePicker
- Objective-C 程序设计 第十二章
- Error While Loading Shared Libraries, Cannot Open Shared Object File
- C++::增加目标码(object code)的大小的操作
- Cannot locate the chosen ObjectFactory implementation: spring - [unknown locati
- struts2中的constant介绍之struts.objectFactory与spring的整合
- Objective-C:init初始化
- Objective-数组操作
- Objective-c 字符串处理
- objective-c系列-单例
- Objective-C中的 id, isa,和 instancetype
- objective-c系列-动态类型和动态绑定
- Object-C(OC)内存管理机制详解 (非原创)