NSJSONSerialization使用

前端之家收集整理的这篇文章主要介绍了NSJSONSerialization使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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];

猜你在找的Json相关文章