30.Swift学习之Codable协议

/ Mac / 没有评论 / 1266浏览

30.Swift学习之Codable协议

Codable协议

import UIKit

//JSON
let res = """
{
"name": "Jone",
"age": 17,
"born_in" : "China",
"sex": "male"
}
"""

//定义结构体实现Codable,一般情况下字段要与JSON的key一致,否则需要额外处理
struct Student: Codable {
    let name: String
    let age: Int
    let born_in :String
    let sex: String
}

//JSON -> 结构体/类 解码 decode
let decoder = JSONDecoder()
do {
    let stu:Student =  try decoder.decode(Student.self, from: res.data(using: .utf8)!)    
    print("name = \(stu.name) , age = \(stu.age) , born_in = \(stu.born_in) , sex = \(stu.sex)")
} catch  {    
    print(error)
}


print("---------------------------")

//结构体/类 -> JSON 编码 encode
let student = Student(name: "zhangsan", age: 20, born_in: "AnHui", sex:"female")
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted

do {
    let data = try encoder.encode(student)  
    print(String(data: data, encoding: .utf8)!)
} catch  {    
    print(error)
}

字段不匹配处理

import UIKit

//JSON
let res = """
{
"name": "Jone",
"age": 17,
"born_in" : "China",
"sex": "male"
}
"""

//定义结构体实现Codable,一般情况下字段要与JSON的key一致,否则需要额外处理
struct Student: Codable {
    let name: String
    let age: Int
    let bornIn :String
    let sex: String
    
    //所有属性写全 会同时影响编码与解码
    enum CodingKeys :String, CodingKey {
        case name
        case age
        case bornIn = "born_in"
        case sex
    }
}

//JSON -> 结构体/类 解码 decode
let decoder = JSONDecoder()
do {
    let stu:Student =  try decoder.decode(Student.self, from: res.data(using: .utf8)!)
    print("name = \(stu.name) , age = \(stu.age) , bornIn = \(stu.bornIn) , sex = \(stu.sex)")
} catch  {
    print(error)
}


print("---------------------------")

//结构体/类 -> JSON 编码 encode
let student = Student(name: "zhangsan", age: 20, bornIn: "AnHui", sex:"female")
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted

do {
    let data = try encoder.encode(student)
    print(String(data: data, encoding: .utf8)!)
    
} catch  {
    print(error)
}

推荐一个软件

开发中推荐使用Paste JSON as Code • quicktype软件,可以根据JSON快速生成Model文件

1