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

NSJSONSerialization JSONObjectWithData options的解析转论坛

发布时间:2020-12-16 19:12:44 所属栏目:百科 来源:网络整理
导读:原文地址:http://www.cocoachina.com/bbs/read.php?tid=110907 我google NSJSONReadingOptions,这帖子出现在第一,但是楼上各位都没把问题说明白。 首先用代码来说明NSJSONReadingMutableContainers的作用: 复制代码 NSString *str = @"{"name":"kaixu

原文地址:http://www.cocoachina.com/bbs/read.php?tid=110907

我google NSJSONReadingOptions,这帖子出现在第一,但是楼上各位都没把问题说明白。


首先用代码来说明NSJSONReadingMutableContainers的作用:
复制代码
  1. NSString *str = @"{"name":"kaixuan_166"}";
  2. NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
  3. // 应用崩溃,不选用NSJSONReadingOptions,则返回的对象是不可变的,NSDictionary
  4. [dict setObject:@"male" forKey:@"sex"];
  5. NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
  6. // 没问题,使用NSJSONReadingMutableContainers,则返回的对象是可变的,NSMutableDictionary
  7. 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

(编辑:李大同)

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

    推荐文章
      热点阅读