加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

NSJSONSerialization使用

发布时间:2020-12-16 19:13:35 所属栏目:百科 来源:网络整理
导读:Objective-C 操作JSON 主要使用的是 NSJSONSerialization 这个类 NSJSONSerialization 包含了以下五个类函数 + (BOOL)isValidJSONObject:(id)obj; 判断 该实例(obj)是否为JSONObject 需满足下面三个条件 1.obj 是NSArray 或 NSDictionay 以及他们派生出来的
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=[[NSMutableDictionaryalloc]init];
  2. [dictionarysetValue:@"xiaominfc"forKey:@"username"];
  3. [dictionarysetValue:@"1991-03-26"forKey:@"birthday"];
  4. [dictionarysetValue:[NSNumbernumberWithInteger:23]forKey:@"age"];
  5. NSArray*arrayOfAnthonysChildren=[[NSArrayalloc]initWithObjects:@"Java",@"Objective-C",@"Python",@"C++",nil];
  6. [dictionarysetValue:arrayOfAnthonysChildrenforKey:@"program_language"];
  7. if([NSJSONSerializationisValidJSONObject:dictionary]){
  8. NSLog(@"itisaJSONObject!");
  9. }
  10. //usedataWithJSONObjectfun
  11. NSError*error=nil;
  12. NSData*jsonData=[NSJSONSerializationdataWithJSONObject:dictionaryoptions:NSJSONWritingPrettyPrintederror:&error];
  13. if([jsonDatalength]>0&&error==nil){
  14. NSString*jsonString=[[NSStringalloc]initWithData:jsonDataencoding:NSUTF8StringEncoding];
  15. NSLog(@"data:%@",jsonString);
  16. //useJSONObjectWithDatafun
  17. NSString*jsonDataString=@"{"username":"xiaominfc","city":"深圳"}";
  18. NSData*data=[jsonDataStringdataUsingEncoding:NSUTF8StringEncoding];
  19. idjsonObject=[NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableContainerserror:&error];
  20. if([jsonObjectisKindOfClass:[NSDictionaryclass]]){
  21. NSDictionary*jsonDictionary=(NSDictionary*)jsonObject;
  22. NSLog(@"username:%@Andcity:%@",[jsonDictionaryvalueForKey:@"username"],[jsonDictionaryvalueForKey:@"city"]);
  23. //usewriteJSONObjectfun
  24. NSString*filePath=@"/Users/xiaominfc/text.txt";
  25. NSOutputStream*outStream=[[NSOutputStreamalloc]initToFileAtPath:filePathappend:NO];
  26. [outStreamopen];
  27. NSIntegerlength=[NSJSONSerializationwriteJSONObject:dictionarytoStream:outStreamoptions:NSJSONWritingPrettyPrintederror:&error];
  28. NSLog(@"write%ldbytes",(long)length);
  29. [outStreamclose];
  30. //useJSONObjectWithStream
  31. NSInputStream*inStream=[[NSInputStreamalloc]initWithFileAtPath:filePath];
  32. [inStreamopen];
  33. idstreamObject=[NSJSONSerializationJSONObjectWithStream:inStreamoptions:NSJSONReadingAllowFragmentserror:&error];
  34. if([streamObjectisKindOfClass:[NSDictionaryclass]]){
  35. NSDictionary*jsonDictionary=(NSDictionary*)streamObject;
  36. NSNumber*ageNumber=(NSNumber*)[jsonDictionaryvalueForKey:@"age"];
  37. NSLog(@"username:%@Andage:%d",[ageNumberintValue]);
  38. [inStreamclose];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读