Chr*_* G. 5 ios alamofire objectmapper
注意:我需要使用 ObjectMapper,而不是 AlamoFireObjectMapper。
如何在不使用 JSON 的情况下从responseJSON 数据映射到用户?以下尝试从responseJSON 到jsonn,然后再次返回到data(作为用户)。我想一定有办法吧?
或者我应该使用 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)
}
Run Code Online (Sandbox Code Playgroud)
map 函数还接受 aJSONObject (Any)或 a 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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4594 次 |
| 最近记录: |