• Swift-JSON Encode && Decode


    typealias Codable = Encodable & Decodable
    
    struct Employee: Codable {
        var name:String
        var id: Int
        var mToy:Toy
    }
    
    struct Toy:Codable {
        var name:String
    }
    
    extension Employee:CustomStringConvertible {
        var description: String {
            return name + "(" + String(id) + "): " + mToy.name
        }
    }
    
    
    
            /// encode instance to jsonString
            let toyA = Toy(name: "Teddy Bear")
            let employeeA = Employee(name: "EmployA", id:1, mToy:toyA)
            let jsonEncoder = JSONEncoder()
            let jsonData = try? jsonEncoder.encode(employeeA)
            // Xcode omits jsonData and only provides the number of bytes in jsonData
            // jsonData contains an unreadable representation of employeeA
            print("jsonData: (jsonData)")
            let jsonString = String(data: jsonData!, encoding: .utf8)
            print("jsonString: (jsonString)")
            
            
            /// decode jsonString to instance
            let jsonDecoder = JSONDecoder()
            let employeeADecoded = try? jsonDecoder.decode(Employee.self, from: jsonData!)
            print(employeeADecoded!)


    //result
    /*

      jsonData: Optional(54 bytes)

      jsonString: Optional("{"name":"EmployA","id":1,"mToy":{"name":"Teddy Bear"}}")

      EmployA(1): Teddy Bear

    */

    假设,现在后台改接口了

    from : {"name":"EmployA","id":1,"mToy":{"name":"Teddy Bear"}}

    to : {"name":"EmployA","gift":"Teddy Bear","emplyeeID":1}

    需要实现encoding 和 decoding的逻辑。

    encode 需要实现 encode(to: Encoder) throws

    decode 需要实现 init(from decoder: Decoder) throws

    //typealias Codable = Encodable & Decodable
    
    struct Employee{
        var name:String
        var id: Int
        var mToy:Toy
        
        enum CodingKeys: String, CodingKey{
            case id = "emplyeeID"
            case name
            case gift
        }
    }
    
    extension Employee: Encodable {
        func encode(to encoder: Encoder) throws {
            var container = encoder.container(keyedBy: CodingKeys.self)
            try container.encode(name, forKey: .name)
            try container.encode(id, forKey: .id)
            try container.encode(mToy.name, forKey: .gift)
        }
    }
    
    extension Employee:Decodable {
        init(from decoder: Decoder) throws {
            let values = try decoder.container(keyedBy: CodingKeys.self)
            name = try values.decode(String.self, forKey: .name)
            id = try values.decode(Int.self, forKey: .id)
            let gift = try values.decode(String.self, forKey: .gift)
            mToy = Toy(name: gift)
        }
    }
    
    extension Employee:CustomStringConvertible {
        var description: String {
            return name + "(" + String(id) + "): " + mToy.name
        }
    }
    
    struct Toy{
        var name:String
    }
  • 相关阅读:
    线性变换
    施密特正交化
    春有它的记忆,秋有它的情怀
    最美的动作其实只需要嘴角上扬-微笑
    pomotime_v1.7.2 番茄软件完全教程
    NGUI 之 不为人知的 NGUITools
    Unity3D 开发 之 加载Android应用的环境
    Unity3D 开发 之 JDK安装与环境变量配置
    Tesseract 对验证码的识别原理和实现步骤
    sizeof_and_strlen 的区别
  • 原文地址:https://www.cnblogs.com/HackHer/p/8507888.html
Copyright © 2020-2023  润新知