簡體   English   中英

通過ObjectMapper映射Alamofire responseJSON

[英]Mappting Alamofire responseJSON via ObjectMapper

注意:我需要使用ObjectMapper,而不是AlamoFireObjectMapper。

你如何映射而不是去JSON但是從responseJSON數據映射到用戶? 以下是嘗試從responseJSON轉到jsonn然后再返回到數據(是User)。 我想一定有辦法嗎?

或者我應該使用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)

   }

map函數還接受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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM