Objective-C JSON操作

本文介绍了Objective-C中处理JSON的主要方法,使用NSJSONSerialization类进行JSON对象与NSData、NSString之间的转换,包括数据验证、编码与解码、写入输出流以及从输入流读取。通过代码示例展示了如何在Objective-C中进行有效的JSON操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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代码  收藏代码
  1. NSMutableDictionary *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 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] && 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]; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子木潇雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值