JSON参数选择

作者: Just_go | 来源:发表于2019-07-14 17:48 被阅读4次

    通常json 反序列化方法:

    let object = try JSONSerialization.jsonObject(with: jsonData, options: [])
    

    options 有这些:

    @available(iOS 5.0, *)
        public struct ReadingOptions : OptionSet {
    
            public init(rawValue: UInt)
    
            public static var mutableContainers: JSONSerialization.ReadingOptions { get }
    
            public static var mutableLeaves: JSONSerialization.ReadingOptions { get }
    
            public static var allowFragments: JSONSerialization.ReadingOptions { get }
        }
    
    1. mutableContainers:
      在 OC 中, 如果想用 mutableArray 或者 mutableDictionary 接收, 必须使用这个参数。
      在 swift 中用 var 修饰的对象就是可变, 所以不需要在转换的时候指定
    2. allowFragments:
      如果要转换的 data 不是 array, 也不是 dictionary, 就需要用这个 option 了, 不然报
    Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.
    
    1. mutableLeaves:
      Guarantees the NSStrings contained in the result must be NSMutableStrings. Rarely used even in old Objective-C codes, ignore it.

    相关文章

      网友评论

        本文标题:JSON参数选择

        本文链接:https://www.haomeiwen.com/subject/lzlikctx.html