Mappting Alamofire responseJSON via ObjectMapper

Note. I need to use ObjectMapper, not AlamoFireObjectMapper.

How do you make a map without going to JSON, but from responseJSON data to map to the user? The following tries to move from responseJSON to jsonn and then revert back to the data (being the user). I think there should be a way?

Or should I use responseData from AlamoFire?

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)

   }
+4
source share
1 answer

The card function also accepts JSONObject (Any)or JSON dictionary ([String: Any]). Depending on the type of response object, you should use the appropriate method.

In your case, use the code below to display JSONResponse

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

Source: https://habr.com/ru/post/1673428/


All Articles