2017-03-28 107 views
3

注意:我需要使用ObjectMapper,而不是AlamoFireObjectMapper。通過ObjectMapper映射Alamofire responseJSON

您如何映射而不使用JSON,而是從responseJSON數據映射到用戶?以下是試圖從responseJSON到jsonn,然後再回到數據(是用戶)。我猜這肯定有辦法嗎?

還是應該使用AlamoFire的responseData?

public class User: Mappable { 
    var username: String? 

    required public init?(map: Map) { 

    } 

    // Mappable 
    public func mapping(map: Map) { 
     username <- map["username"] 
    } 
} 


    Alamofire.request(
     url, 
     method: .get, 
     headers: headers_employees).responseJSON { 
      response in 
     // Map via ObjectMapper 
     let [User] = Mapper<User>().map(JSONString: JSONString) 

    } 

回答

2

地圖功能還接受JSONObject (Any)JSON dictionary ([String: Any])。根據響應對象的類型,您應該使用適當的方法。

在你的情況使用下面的代碼映射JSONResponse

Alamofire.request(
    url, 
    method: .get, 
    headers: headers_employees).responseJSON { 
     response in 
    // Map via ObjectMapper 
    let [User] = Mapper<User>().map(JSONObject:response.result.value) 
}