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];

相关文章

  jsonp需要在页面中添加一个<script>元素,由该元素来从其他服务器加载json数据。 <body&g...
<script> var testApi = "地址"; $.ajax({ url:testApi,//可以不是本地域名 type:‘post...
总是有人会遇到跨域问题,然后有个jsonp的解决方案,MVC中代码如下: public class JsonpResult : Syst...
最近开发中遇到调用第三方web_api的功能,后端在处理json数据时使用fastjson来做反序列化,由于调用api...
JSON全称为JavaScript ObjectNotation,它是一种轻量级的数据交换格式,易于阅读、编写、解析。jsoncpp...
JsonSerializer有多个属性,用于自定义如何序列化JSON。这些也可以通过JsonSerializerSettings参数,在...