关于NSJSONReadingOptions参数的含义
2015-12-11 15:17
309 查看
JSONObjectWithData:options:error:方法来进行数据转换,这里的options是一个枚举值,官方文档定义是这样
enum {
NSJSONReadingMutableContainers = (1UL << 0),
NSJSONReadingMutableLeaves = (1UL << 1),
NSJSONReadingAllowFragments = (1UL << 2)
};
这三个的区别含义如下:
首先用代码来说明NSJSONReadingMutableContainers的作用:
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
enum {
NSJSONReadingMutableContainers = (1UL << 0),
NSJSONReadingMutableLeaves = (1UL << 1),
NSJSONReadingAllowFragments = (1UL << 2)
};
这三个的区别含义如下:
NSJSONReadingMutableContainers Specifies that arrays and dictionaries are created as mutable objects. // 创建可变的数组或字典 接收 NSJSONReadingMutableLeaves Specifies that leaf strings in the JSON object graph are created as instances of NSMutableString. // 指定在JSON对象可变字符串被创建为NSMutableString的实例 NSJSONReadingAllowFragments Specifies that the parser should allow top-level objects that are not an instance of NSArray or NSDictionary // 指定解析器应该允许不属于的NSArray或NSDictionary中的实例顶层对象
首先用代码来说明NSJSONReadingMutableContainers的作用:
NSString *str = @"{\"name\":\"kaixuan_166\"}"; NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; // 应用崩溃,不选用NSJSONReadingOptions,则返回的对象是不可变的,NSDictionary [dict setObject:@"male" forKey:@"sex"]; NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil]; // 没问题,使用NSJSONReadingMutableContainers,则返回的对象是可变的,NSMutableDictionary [dict setObject:@"male" forKey:@"sex"]; 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
相关文章推荐
- Puppet Openstack Mitaka Design Summit小结
- Tomcat翻译--Context Container
- Centos中使用yum安装java时,没有jps的问题的解决。
- tomcat下发布项目,遇到的问题总结
- 搭建linux 0.11内核编译环境(转)
- billy启动tomcat配置
- 测可用!ecshop立即购买和加入购物车按钮共存的方法
- tomcat优化(三)web运用动静分类后,解决js跨域问题方案
- Linux -- 系统在线更新yum与apt-get详细讲解
- Tomcat翻译--The Host Container
- linux配置open*** 客户端windows
- 分层架构与公司组织
- Gitlab代码管理平台部署(CentOS6.7—gitlab7.2.12)
- Gitlab代码管理平台部署(CentOS6.7―gitlab7.2.12)
- powershell 汉洛塔
- Linux下使用rsync最快速删除海量文件的方法
- linux: 鸟哥的私房菜
- Linux下Nodejs安装(完整详细)
- linux shortcut
- 在openwrt上实现用手机进行3G拨号