【问题标题】:Mappting Alamofire responseJSON via ObjectMapper通过 ObjectMapper 映射 Alamofire responseJSON
【发布时间】:2017-08-21 21:50:27
【问题描述】:

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

如何在不使用 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)

   }

【问题讨论】:

    标签: ios alamofire objectmapper


    【解决方案1】:

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 2018-03-28
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多