NSJSONSerialization类的使用
2015-09-21 14:00
609 查看
(1)概述
iOS实现json解析的第三方库也是挺多的,但原生的NSJSONSerialization类既使用方便而性能又是比较好的,所以推荐在iOS端上实现json的解析与转换还是使用NSJSONSerialization比较好。
(2)实现json的解析
NSJSONSerialization类的+ (id)JSONObjectWithData:(NSData
*)data options:(NSJSONReadingOptions)opt error:(NSError
**)error;静态方法能实现json解析
有时,json数据中还会包含json数据,这种情况还需要将解析出来的数据再进行一次json解析。
其中NSJSONReadingOptions的枚举结构为:
typedef
NS_OPTIONS(NSUInteger, NSJSONReadingOptions) {
NSJSONReadingMutableContainers = (1UL <<
0),
NSJSONReadingMutableLeaves = (1UL
<< 1),
NSJSONReadingAllowFragments = (1UL
<< 2)
}
NS_ENUM_AVAILABLE(10_7,
5_0);
NSJSONReadingMutableContainers:Specifies
that arrays and dictionaries are created as mutable objects.
//返回NSArray与NSDictionary的可变容器
NSJSONReadingMutableLeaves:Specifies that leaf strings in the
JSON object graph are created as instances of NSMutableString.
//json中的字符串对象均返回可变对象。
NSJSONReadingAllowFragments:Specifies that the parser should
allow top-level objects that are not an instance of NSArray or NSDictionary.
//允许最外层对象非NSArray或NSDictionary
(3)实现json的生成
+ (NSData
*)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt
error:(NSError **)error; 通过该方法可以实现将对象转成json数据。
iOS实现json解析的第三方库也是挺多的,但原生的NSJSONSerialization类既使用方便而性能又是比较好的,所以推荐在iOS端上实现json的解析与转换还是使用NSJSONSerialization比较好。
(2)实现json的解析
NSJSONSerialization类的+ (id)JSONObjectWithData:(NSData
*)data options:(NSJSONReadingOptions)opt error:(NSError
**)error;静态方法能实现json解析
有时,json数据中还会包含json数据,这种情况还需要将解析出来的数据再进行一次json解析。
其中NSJSONReadingOptions的枚举结构为:
typedef
NS_OPTIONS(NSUInteger, NSJSONReadingOptions) {
NSJSONReadingMutableContainers = (1UL <<
0),
NSJSONReadingMutableLeaves = (1UL
<< 1),
NSJSONReadingAllowFragments = (1UL
<< 2)
}
NS_ENUM_AVAILABLE(10_7,
5_0);
NSJSONReadingMutableContainers:Specifies
that arrays and dictionaries are created as mutable objects.
//返回NSArray与NSDictionary的可变容器
NSJSONReadingMutableLeaves:Specifies that leaf strings in the
JSON object graph are created as instances of NSMutableString.
//json中的字符串对象均返回可变对象。
NSJSONReadingAllowFragments:Specifies that the parser should
allow top-level objects that are not an instance of NSArray or NSDictionary.
//允许最外层对象非NSArray或NSDictionary
(3)实现json的生成
+ (NSData
*)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt
error:(NSError **)error; 通过该方法可以实现将对象转成json数据。
相关文章推荐
- JSP Servlet学习笔记——使用fileupload上传文件
- 转化为json字符串
- 关于js中select的简单操作,以及js前台计算,span简单操作
- JS学习总结之Core DOM
- JavaScript之array
- js之DOM操作(插入节点insertBefore())
- jsp中的<%=表达式%>和<%表达式%>有什么区别?
- 弱问JSP中的<%= %>和${ }到底有什么区别?
- JavaScript验证Email(3种方法)
- js常用插件
- 基于jsp+servlet实现的简单博客系统实例(附源码)
- Javascript模块化编程(三):require.js的用法
- go json
- JSON解析字符串
- Javascript模块化编程(二):AMD规范
- Ajax-Js实现
- 解决JS浮点数(小数)计算加减乘除的BUG
- JS 处理浮点型问题
- js之DOM操作(访问子节点和最后一个节点)
- javascript 继承2 总结