NSJSONSerialization
Inherits from: NSObject
Confroms to: NSObject
Framework: Foundation in iOS5.0 and later
You use the NSJSONSerialization class to convert JSON to Foundation objects and convert Foundation objects to JSON.
An object that may be converted to JSON must have the following properties:
- The top level object is an NSArray or NSDictionary.
- All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
- All dictionary keys are instances of NSString.
- Numbers are not NaN or infinity.
Other rules may apply. Calling isValidJSONObject: or attempting a conversion are the definitive ways to tell if a given object can be converted to JSON data.
你可以使用NSJSONSerialization类来将JSON格式的数据转换成基础库对象,或者将基础库对象转换成JSON格式的数据。
一个能被转换成JSON格式的对象必须遵循以下条件:
- 顶级对象是一个NSArray或者NSDictionary对象(可以简单理解为转换的素材必须为一个数组或者字典)
- 所有次级对象必须是NSString, NSNumber, NSArray, NSDictionary, 或NSNull的实例
- 所有字典的key必须是NSString的实例
- 数字必须不为NaN或者无数(inf一般是因为得到的数值,超出浮点数的表示范围(溢出,即阶码部分超过其能表示的最大值),而NaN一般是因为对浮点数进行了未定义的操作,如对-1开方。)
JSON格式数据对象化
+ JSONObjectWithData:options:error:
根据给出的JSON数据返回一个基础库对象
声明
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError * _Nullable *)error
参数
data | A data object containing JSON data. //一个包含JSON格式数据的NSData对象 |
opt |
Options for reading the JSON data and creating the Foundation objects. //解析JSON数据并创建基础库对象的选择项 For possible values, see NSJSONReadingOptions. |
error |
If an error occurs, upon return contains an NSError object that describes the problem. //如果出错,返回NSError对象来描述问题所在 |
常量
NSJSONReadingOptions
enum { NSJSONReadingMutableContainers = (1UL << 0), //将生成可变数组、字典类型 NSJSONReadingMutableLeaves = (1UL << 1), //将JSON对象图中的字符串解析为可变字符串类型 NSJSONReadingAllowFragments = (1UL << 2) //拆分解析,静态语法检测将忽略顶级对象不是一个数组或者字典的事实 }; typedef NSUInteger NSJSONReadingOptions;
+ JSONObjectWithStream:options:error:
从一个JSON数据的文件读入流中返回基础库对象
声明
+ (id)JSONObjectWithStream:(NSInputStream *)stream options:(NSJSONReadingOptions)opt error:(NSError * _Nullable *)error
参数
stream |
A stream from which to read JSON data. //读取JSON数据的读入流 The stream should be opened and configured. |
opt |
Options for reading the JSON data and creating the Foundation objects. //解析JSON数据并创建基础库对象的选择项 For possible values, see NSJSONReadingOptions. |
error | If an error occurs, upon return contains an NSError object that describes the problem. //如果出错,返回NSError对象来描述问题所在 |
对象转JSON格式数据
+ dataWithJSONObject:options:error:
根据基础库对象返回JSON格式数据
声明
+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError * _Nullable *)error
参数
obj |
The object from which to generate JSON data. Must not be nil. //用来生成JSON数据的对象(顶级、次级)不可为nil |
opt |
Options for creating the JSON data. //生成JSON数据的选择项 See NSJSONWritingOptions for possible values. Pass 0 to specify no options. |
error | If an error occurs, upon return contains an NSError object that describes the problem. //如果出错,返回NSError对象来描述问题所在 |
常量
NSJSONWritingOptions
enum { NSJSONWritingPrettyPrinted = (1UL << 0) }; typedef NSUInteger NSJSONWritingOptions; //JSON数据将自动添加空白字符,以使输出更具可读性。如果没有设置这个选项,JSON数据将以尽可能紧凑的格式生成。
+ writeJSONObject:toStream:options:error:
通过输入流返回JSON数据
与上述各方法较为雷同,下略不累述
+ isValidJSONObject:
判断一个对象是否能被转换成JSON数据,返回一个布尔值
声明
+ (BOOL)isValidJSONObject:(id)obj
参数
obj |
The object to test. //被测试的对象 |