为有牺牲多壮志,敢教日月换新天。

[Swift]Swift原生:JSON转换Model

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(let_us_code)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https:////www.cnblogs.com/strengthen/p/12381682.html
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Apple 在 Swift  的 Foundation 的模块中添加了对 JSON 解析成Model的原生支持。

虽然之前也有ObjectMapperSwiftyJSON这样的解决方案,但是Swift 中添加了原生支持还是不免让人兴奋,使用起来也相对方便了许多。

简单使用

如果你的 JSON 数据结构和你使用的 Model 对象结构一致的话,那么解析过程将会非常简单。只需要让Model声明 Codable 协议即可。

JSON数据:

 1 {
 2     "userInfos": [
 3         {
 4             "userName": "小名",
 5             "age": 18,
 6             "height": 178.56,
 7             "sex": true
 8         },
 9         {
10             "userName": "小方",
11             "age": 18,
12             "height": 162.56,
13             "sex": false
14         }
15     ]
16 }

Model对象:

 1 struct UserList: Codable {
 2 
 3     let userInfos: [UserInfo]
 4 
 5     struct UserInfo: Codable {
 6 
 7         let userName: String
 8         let age: Int
 9         let height: Float
10         let sex: Bool
11     }
12 }

JSON转Model:

1     let jsonDecoder = JSONDecoder()
2     let modelObject = try? jsonDecoder.decode(UserList.self, from: jsonData)

Model转JSON:

1     let jsonEncoder = JSONEncoder()
2     let jsonData = try? jsonEncoder.encode(modelObject)

关于 Codable

1 /// A type that can convert itself into and out of an external representation.
2 public typealias Codable = Decodable & Encodable

Codable 同时声明了 Decodable 和 Encodable 两个协议,只需要单向处理的话可以只声明其中一个协议。

需要注意的是,使用时必须保证 JSON 数据结构和你使用的 Model 对象结构一致,任何不一致都会导致解析失败。

但是实际开发过程中,往往不会总是这样满足我们的需要,有时也需要我们对解析过程做一些改变。

自定义键值名

为了保持风格一致,我们有时候不得不改变key成为我们需要的key值。

仍然使用上面的例子,现在我们把Model中的key height 改为 bodyHeight

 1 struct UserList: Codable {
 2 
 3     let userInfos: [UserInfo]
 4     
 5     struct UserInfo: Codable {
 6         
 7         let userName: String
 8         let age: Int
 9         let bodyHeight: Float
10         let sex: Bool
11         
12         private enum CodingKeys: String, CodingKey {
13             case userName
14             case age
15             case bodyHeight = "height"
16             case sex
17         }
18     }
19 }

格式处理

JSONEncoder 和 JSONDecoder 本身也提供了对时间格式的处理,Data的处理,浮点不匹配的处理。可以根据自己的需要设置自己想要的格式。

1     /// The strategy to use in decoding dates. Defaults to `.deferredToDate`.
2     open var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy
3 
4     /// The strategy to use in decoding binary data. Defaults to `.base64`.
5     open var dataDecodingStrategy: JSONDecoder.DataDecodingStrategy
6 
7     /// The strategy to use in decoding non-conforming numbers. Defaults to `.throw`.
8     open var nonConformingFloatDecodingStrategy: JSONDecoder.NonConformingFloatDecodingStrategy

JSONEncoder 在生成 JSON 的时候默认的格式是

{"userInfos":[{"age":18,"sex":true,"height":178.55999755859375,"userName":"小名"},{"age":18,"sex":false,"height":162.55999755859375,"userName":"小方"}]}

如果想要提高阅读体验, 可以设置属性 outputFormatting

1 open var outputFormatting: JSONEncoder.OutputFormatting
2 
3 let jsonEncoder = JSONEncoder()
4 jsonEncoder.outputFormatting = .prettyPrinted

然后生成的JSON格式就会变成这样

 1 {
 2   "userInfos" : [
 3     {
 4       "age" : 18,
 5       "sex" : true,
 6       "height" : 178.55999755859375,
 7       "userName" : "小名"
 8     },
 9     {
10       "age" : 18,
11       "sex" : false,
12       "height" : 162.55999755859375,
13       "userName" : "小方"
14     }
15   ]
16 }

JSON中 没有 Model 所需要的 Key

如果JSON的数据结构中,有些 Key - Value 不一定出现,Model中对应的要实用可选类型

JSON数据:

 1 {
 2     "userInfos": [
 3         {
 4             "userName": "小名",
 5             "age": 18,
 6             "sex": true
 7         },
 8         {
 9             "userName": "小方",
10             "age": 18,
11             "height": 162.56,
12             "sex": false
13         }
14     ]
15 }

Model对象:

 1 struct UserList: Codable {
 2 
 3     let userInfos: [UserInfo]
 4 
 5     struct UserInfo: Codable {
 6 
 7         let userName: String
 8         let age: Int
 9         let height: Float?
10         let sex: Bool
11     }
12 }

 

posted @ 2020-02-29 10:14  为敢技术  阅读(6787)  评论(0编辑  收藏  举报