1、NSPropertyListSerialization
Foundation框架提供了 NSPropertyListSerialization 类处理plist文件
list 对象指 NSString,NSArray,NSDictionary,NSDate,NSNumber等数据形式
NSPropertyListSerialization 的作用:
- list对象转换NSData
+ (nullable id)propertyListWithData:(NSData *)data
options:(NSPropertyListReadOptions)opt
format:(nullable NSPropertyListFormat *)format
error:(out NSError **)error
- NSData转换list对象
+ (nullable NSData *)dataWithPropertyList:(id)plist
format:(NSPropertyListFormat)format
options:(NSPropertyListWriteOptions)opt
error:(out NSError **)error
options选项类型:
typedef NS_OPTIONS(NSUInteger, NSPropertyListMutabilityOptions) {
NSPropertyListImmutable = kCFPropertyListImmutable,
NSPropertyListMutableContainers = kCFPropertyListMutableContainers,
NSPropertyListMutableContainersAndLeaves = kCFPropertyListMutableContainersAndLeaves
};
NSPropertyListImmutable 属性列表包含不可变对象
NSPropertyListMutableContainers 属性列表父节点是可变的类型,子节点是不可变类型
NSPropertyListMutableContainersAndLeaves 属性列表父节点和子节点都是可变的类型
format格式类型:
typedef NS_ENUM(NSUInteger, NSPropertyListFormat) {
NSPropertyListOpenStepFormat = kCFPropertyListOpenStepFormat,
NSPropertyListXMLFormat_v1_0 = kCFPropertyListXMLFormat_v1_0,
NSPropertyListBinaryFormat_v1_0 = kCFPropertyListBinaryFormat_v1_0
};
NSPropertyListXMLFormat_v1_0 指定属性列表文件格式为XML格式,仍然是纯文本类型,不会压缩文件
NSPropertyListBinaryFormat_v1_0 指定属性列表文件格式为二进制格式,文件是二进制类型,会压缩文件
NSPropertyListOpenStepFormat 指定属性列表文件格式为ASCII码格式,对于旧格式的属性列表文件,不支持写入操作
2、NSJSONSerialization
Foundation框架提供了 NSJSONSerialization 类对JSON进行序列化处理
转换成JSON的对象必须具有如下属性:
- 顶层对象必须是NSArray或者NSDictionary
- 所有的对象必须是NSString、NSNumber、NSArray、NSDictionary、NSNull
- 所有NSDictionary的key必须是NSString类型
- 数字对象不能是非数值或无穷(NSNumber)
NSJSONSerialization的作用:
检测Foundation对象能否合法转换为JSON
+ (BOOL)isValidJSONObject:(id)obj;
JSON数据对象转换为NSData
+ (nullable NSData *)dataWithJSONObject:(id)obj
options:(NSJSONWritingOptions)opt
error:(NSError **)error
NSData转换为JSON数据对象
+ (nullable id)JSONObjectWithData:(NSData *)data
options:(NSJSONReadingOptions)opt
error:(NSError **)error
options选项类型
typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) {
NSJSONReadingMutableContainers = (1UL << 0),
NSJSONReadingMutableLeaves = (1UL << 1),
NSJSONReadingFragmentsAllowed = (1UL << 2),
NSJSONReadingAllowFragments API_DEPRECATED_WITH_REPLACEMENT("NSJSONReadingFragmentsAllowed", macos(10.7, API_TO_BE_DEPRECATED), ios(5.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED)) = NSJSONReadingFragmentsAllowed,
}
NSJSONReadingMutableContainers 将解析的数组和字典设置为可变对象(NSMutableDictionary或NSMutableArray)
NSJSONReadingMutableLeaves 将解析数据的子节点创建为可变字符串对象(NSMutableString)
NSJSONReadingAllowFragments 允许解析对象的最上层不是字典或者数组 ,但必须是有效的JSON Fragment
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.
*/
NSJSONWritingSortedKeys API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0)) = (1UL << 1),
NSJSONWritingFragmentsAllowed = (1UL << 2),
NSJSONWritingWithoutEscapingSlashes API_AVAILABLE(macos(10.15), ios(13.0), watchos(6.0), tvos(13.0)) = (1UL << 3),
}
NSJSONWritingPrettyPrinted 将生成的json数据格式化输出(空格和缩进),这样可读性高,否则输出的json就是一整行
NSJSONWritingSortedKeys 对key进行排序后输出
NSJSONWritingWithoutEscapingSlashes 不转义斜杠(/)
将JSON的NSData转换为NSString
NSString *jsonstr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];