iOS 之NSJSONReadingOptions说明【转】

首先用代码来说明NSJSONReadingMutableContainers的作用:

 1     NSString *str = @"{\"name\":\"kaixuan_166\"}";
 2     
 3     NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
 4     // 应用崩溃,不选用NSJSONReadingOptions,则返回的对象是不可变的,NSDictionary
 5     [dict setObject:@"male" forKey:@"sex"];
 6     
 7     NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
 8     // 没问题,使用NSJSONReadingMutableContainers,则返回的对象是可变的,NSMutableDictionary
 9     [dict setObject:@"male" forKey:@"sex"];
10     
11     NSLog(@"%@", dict);

NSJSONReadingMutableContainers:返回可变容器,NSMutableDictionary或NSMutableArray。 
 
NSJSONReadingMutableLeaves:返回的JSON对象中字符串的值为NSMutableString,目前在iOS 7上测试不好用,应该是个bug,参见: 
http://stackoverflow.com/questions/19345864/nsjsonreadingmutableleaves-option-is-not-working 
 
NSJSONReadingAllowFragments:允许JSON字符串最外层既不是NSArray也不是NSDictionary,但必须是有效的JSON Fragment。例如使用这个选项可以解析 @“123” 这样的字符串。参见: 
http://stackoverflow.com/questions/16961025/nsjsonserialization-nsjsonreadingallowfragments-reading 

 

转自:http://www.cocoachina.com/bbs/read.php?tid=110907#959188

转载于:https://www.cnblogs.com/linyc/p/4272060.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值