您当前的位置: 首页 >  json

培根芝士

暂无认证

  • 0浏览

    0关注

    446博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

iOS NSPropertyListSerialization和NSJSONSerialization

培根芝士 发布时间:2020-04-23 11:37:28 ,浏览量:0

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             
关注
打赏
1660824269
查看更多评论
0.0421s