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];