NSJSONReadingOptions
typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) {
NSJSONReadingMutableContainers = (1UL << 0),
NSJSONReadingMutableLeaves = (1UL << 1),
NSJSONReadingAllowFragments = (1UL << 2)
} API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
- NSJSONReadingMutableContainers = (1UL << 0), // 返回可变容器,NSMutableDictionary或NSMutableArray.
- NSJSONReadingMutableLeaves = (1UL << 1), // 不仅返回的最外层是可变的, 内部的子数值或字典也是可变对象.
- NSJSONReadingAllowFragments = (1UL << 2) // 返回允许JSON字符串最外层既不是NSArray也不是NSDictionary,但必须是有效的JSON Fragment.可以是如 “10”,或者直接是字符串或者NSNumber.
NSJSONWritingOptions
typedef NS_OPTIONS(NSUInteger, NSJSONWritingOptions) {
NSJSONWritingPrettyPrinted = (1UL << 0),
/* Sorts dictionary keys for output using [NSLocale systemLocale]. Keys are compared using NSNumericSearch. The specific sorting method used is subject to change.
使用NSLocale systemLocale对输出的字典键进行排序。使用NSNumericSearch数值搜索来比较键。所使用的特定排序方法会发生变化
*/
NSJSONWritingSortedKeys API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0)) = (1UL << 1)
} API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
- NSJSONWritingPrettyPrinted = (1UL << 0) //是将生成的json数据格式化输出,这样可读性高,不设置则输出的json字符串就是一整行。
- NSJSONWritingSortedKeys //输出的json字符串就是一整行.
JSON数据(NSData) -> OC对象(Foundation Object)
{} -> NSDictionary @{}
[] -> NSArray @[]
“jack” -> NSString @“jack”
10 -> NSNumber @10
true -> NSNumber @1
false -> NSNumber @0
null -> NSNull
应用
// 利用NSJSONSerialization类
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
** OC对象(Foundation Object)-> JSON数据(NSData)**
应用
// 利用NSJSONSerialization类
+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;