NSJSONSerialization使用

虞俊美
2023-12-01
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 并返回



  1. SMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];  
  2.        [dictionary setValue:@"xiaominfc" forKey:@"username"];  
  3.        [dictionary setValue:@"1991-03-26" forKey:@"birthday"];  
  4.        [dictionary setValue:[NSNumber numberWithInteger:23] forKey:@"age"];  
  5.        NSArray *arrayOfAnthonysChildren = [[NSArray alloc]initWithObjects:@"Java",@"Objective-C",@"Python",@"C++", nil];  
  6.        [dictionary setValue:arrayOfAnthonysChildren forKey:@"program_language"];  
  7.          
  8.        if([NSJSONSerialization isValidJSONObject:dictionary]){  
  9.            NSLog(@"it is a JSONObject!");  
  10.        }  
  11.        //use dataWithJSONObject fun  
  12.          
  13.        NSError *error = nil;  
  14.        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];  
  15.        if([jsonData length] > 0 && error == nil) {  
  16.            NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];  
  17.            NSLog(@"data:%@",jsonString);  
  18.              
  19.        }  
  20.          
  21.        //use JSONObjectWithData fun  
  22.          
  23.        NSString *jsonDataString = @"{\"username\":\"xiaominfc\",\"city\":\"深圳\"}";  
  24.        NSData *data = [jsonDataString dataUsingEncoding:NSUTF8StringEncoding];  
  25.        id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];  
  26.        if ([jsonObject isKindOfClass:[NSDictionary class]]) {  
  27.            NSDictionary *jsonDictionary = (NSDictionary*)jsonObject;  
  28.            NSLog(@"username:%@ And city:%@",[jsonDictionary valueForKey:@"username"],[jsonDictionary valueForKey:@"city"]);  
  29.        }  
  30.          
  31.        //use writeJSONObject fun  
  32.          
  33.        NSString *filePath = @"/Users/xiaominfc/text.txt";  
  34.          
  35.        NSOutputStream *outStream = [[NSOutputStream alloc]initToFileAtPath:filePath append:NO];  
  36.        [outStream open];  
  37.        NSInteger length = [NSJSONSerialization writeJSONObject:dictionary toStream:outStream options:NSJSONWritingPrettyPrinted error:&error];  
  38.        NSLog(@"write %ld bytes",(long)length);  
  39.        [outStream close];  
  40.          
  41.        //use JSONObjectWithStream  
  42.        NSInputStream *inStream = [[NSInputStream alloc]initWithFileAtPath:filePath];  
  43.        [inStream open];  
  44.        id streamObject = [NSJSONSerialization JSONObjectWithStream:inStream options:NSJSONReadingAllowFragments error:&error];  
  45.        if ([streamObject isKindOfClass:[NSDictionary class]]) {  
  46.            NSDictionary *jsonDictionary = (NSDictionary*)streamObject;  
  47.            NSNumber *ageNumber = (NSNumber*)[jsonDictionary valueForKey:@"age"];  
  48.            NSLog(@"username:%@ And age:%d",[jsonDictionary valueForKey:@"username"],[ageNumber intValue]);  
  49.        }  
  50.        [inStream close];  

 类似资料: