How to make model class for following JSON response in swift iOS

前端 未结 6 1711
花落未央
花落未央 2021-02-10 14:40

Hi i am beginner for swift ios and my requirement is have to display Json response to table list i got response from web-services and response seems like below

MY requ

6条回答
  •  一生所求
    2021-02-10 14:46

    Model class:

    class test : Unboxable {
    let id : String
    let imageURl : String
    let name : String
    let hasVip : Bool
    let city : String
    
    required init(unboxer: Unboxer) throws {
        self.id = unboxer.unbox(key: "id") ?? ""
        self.imageURl = unboxer.unbox(key: "imageUrl") ?? ""
        self.name = unboxer.unbox(key: "name") ?? ""
        self.hasVip = unboxer.unbox(key: "hasVip") ?? false
        self.city = (unboxer.unbox(key: "city") ?? nil)!
    }
    }
    

    parse json:

    if let object = json as? [Any] {
                    // json is an array
    
                    var data :[test] = []
                    for respObject in object {
    
                        var dict = respObject as? Dictionary
                        dict?["city"] = dict?["location"]?["city"] as AnyObject
                         let result1: [test] = try unbox(dictionaries: [dict!])
                        data.append(contentsOf: result1)
    
                    }
    
                    print(data)
    

    For more details follow https://github.com/JohnSundell/Unbox

提交回复
热议问题