将json集合响应映射到swift对象类的简单方法 [英] simple way to map a json collection response into swift object class

查看:143
本文介绍了将json集合响应映射到swift对象类的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多库,Alamofire,JsonHelper,ObjectMapper等...,但是不幸的是,Ive并未将json集合响应映射到对象类中. 我正在使用swift 1.2和xcode 6.3开发IOS 8 App,我的模型有两类:

Ive tried a lot of libraries, Alamofire, JsonHelper, ObjectMapper etc..., but unfortunately, Ive coundn't map a json collection response into an object class. Im developing an IOS 8 App with swift 1.2 and xcode 6.3, and two classes of my model are:

Club.swift

Club.swift

class Club { 

    var id: String = ""
    var name: String = ""
    var imageUrl: String = ""
    var hasVip: Bool = false
    var desc: String = ""
    var location: [Location] = []

}

Location.swift

Location.swift

class Location {

    var country: String = ""
    var city: String = ""
    var address: String = ""
    var zip: String = ""
    var underground: [String] = []

}

我还有另一个要向我的API请求的类:

I have another class to request to my API:

apliClient.swift

apliClient.swift

class ApiClient {

    var clubs = [Club]?()


    func getList(completionHandler: ([JSON]) -> ()) {
        let URL = NSURL(string: "https://api.com/v1/clubs")
        let mutableURLRequest = NSMutableURLRequest(URL: URL!)

        mutableURLRequest.setValue("Content-Type", forHTTPHeaderField: "application/json")
        mutableURLRequest.HTTPMethod = "GET"
        mutableURLRequest.setValue("Bearer R01.iNsG3xjv/r1LDkhkGOANPv53xqUFDkPM0en5LIDxx875fBjdUZLn1jtUlKVJqVjsNwDe1Oqu2WuzjpaYbiWWhw==", forHTTPHeaderField: "Authorization")

        let manager = Alamofire.Manager.sharedInstance
        let request = manager.request(mutableURLRequest)

        request.responseJSON { (request, response, json , error) in
            if (json != nil){
                var jsonObj = JSON(json!)
                if let data = jsonObj["hits"].arrayValue as [JSON]?{
                    completionHandler(data)
                }
            }
        }
    }
}

我认为,有一种简单的方法可以快速映射对象.我想知道如何返回已转换为[Club]对象的完成处理程序(数据)?

and I think, there is a simple way to mapping objects in swift. I would like to know, how I can return the completionHandler(data) converted into a [Club] object?

让数据 = jsonObj ["hits"].arrayValue为[JSON]?

let data = jsonObj["hits"].arrayValue as [JSON]? is

[{
  "_id" : "5470def9e0c0be27780121d7",
  "imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/5470def9e0c0be27780121d7_180.png",
  "name" : "Mondo",
  "hasVip" : false,
  "location" : {
    "city" : "Madrid"
  }
}, {
  "_id" : "540b2ff281b30f3504a1c72f",
  "imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/540b2ff281b30f3504a1c72f_180.png",
  "name" : "Teatro Kapital",
  "hasVippler" : false,
  "location" : {
    "address" : "Atocha, 125",
    "city" : "Madrid"
  }
}, {
  "_id" : "540cd44581b30f3504a1c73b",
  "imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/540cd44581b30f3504a1c73b_180.png",
  "name" : "Charada",
  "hasVippler" : false,
  "location" : {
    "address" : "La Bola, 13",
    "city" : "Madrid"
  }
}]

推荐答案

您可以使用上面提到的ObjectMapper库执行此操作.只需创建Club类并确保其实现了Mappable协议.然后,您可以按以下方式使用ObjectMapper来映射数据:

You can do this with the ObjectMapper library that you mentioned above. Simply create the Club class and make sure it implements the Mappable protocol. Then you can use ObjectMapper as follows to map the data:

let clubs = Mapper<Club>().mapArray(JSONString)

完全公开:我是ObjectMapper的作者.

Full disclosure: I am the author of ObjectMapper.

这篇关于将json集合响应映射到swift对象类的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆