如何使用对象映射器从响应 JSON 映射子数组?
Posted
技术标签:
【中文标题】如何使用对象映射器从响应 JSON 映射子数组?【英文标题】:How to map child array from the response JSON using object mapper? 【发布时间】:2016-12-26 11:05:22 【问题描述】:我正在使用Alamofire
和Alamofire Object Mapper
。
认为这是来自网络服务的响应:
"status": 200,
"error": false,
"response":
"id": 9,
"parent_id": 0,
"company_id": 1,
"image": "",
"name": "Games",
"description": "Games",
"created_at": "2016-12-16 12:11:51",
"updated_at": "2016-12-16 12:11:51",
"deleted_at": null,
"Games": [
"id": 36,
"company_id": 1,
"application_id": 7,
"category_id": 9,
"start_date": "2016-12-16 00:00:00",
"end_date": "2016-12-27 00:00:00",
"status": "PUBLISH",
"created_at": "2016-12-16 13:29:16",
"updated_at": "2016-12-16 13:29:48",
"deleted_at": null
]
,
"error_messages": [],
"error_message": ""
从这个响应中我想得到Games
数组。
这是我的Games
模型
var gameId : String!
var companyID : String!
var categoryID : String!
var startDate : String!
var endDate : String!
var status : String!
var buildertitle : String!
var builderdescription : String!
override init()
super.init()
required convenience init?(map : Map)
self.init()
func mapping(map: Map)
gameId <- map["id"]
companyID <- map["company_id"]
categoryID <- map["category_id"]
startDate <- map["start_date"]
endDate <- map["end_date"]
status <- map["status"]
buildertitle <- map["title"]
builderdescription <- map["description"]
init(dic : NSDictionary)
super.init()
gameId = Utils.nulltoEmpty(dic["id"] as AnyObject) as! String
companyID = Utils.nulltoEmpty(dic["company_id"] as AnyObject) as! String
categoryID = Utils.nulltoEmpty(dic["category_id"] as AnyObject) as! String
startDate = Utils.nulltoEmpty(dic["start_date"] as AnyObject) as! String
endDate = Utils.nulltoEmpty(dic["end_date"] as AnyObject) as! String
status = Utils.nulltoEmpty(dic["status"] as AnyObject) as! String
buildertitle = Utils.nulltoEmpty(dic["title"] as AnyObject) as! String
builderdescription = Utils.nulltoEmpty(dic["description"] as AnyObject) as! String
这是我的response
模型
var status : Int!
var user : User?
var errorMessage : String?
var device : Device?
var games : Games?
required convenience init?(map : Map)
self.init()
func mapping(map: Map)
status <- map["status"]
user <- map["response"]
errorMessage <- map["error_message"]
device <- map["response"]
games <- map["Games"]
最后这是我对 alamofire 的要求
func getGamesList ()
let UrlReqeust = Router.getUserNews().urlRequest
Alamofire.request(UrlReqeust!).responseArray (response : DataResponse<[Games]>) in
print("The status code is : \(response.response?.statusCode)")
print("the response is : \(response)")
switch response.result
case .success(let gamesbuilder):
print("the result is : \(gamesbuilder)")
break
case .failure(let Error):
break
如果我使用responseString
而不是responseArray
,它会给出String
。否则,它会给出如下所示的成功状态代码错误。
"状态码为:可选(200) 响应是:FAILURE: Error Domain=com.alamofireobjectmapper.error Code=2“ObjectMapper 无法序列化响应。” UserInfo=NSLocalizedFailureReason=ObjectMapper 序列化响应失败。"
另一部分是,所有responses
都在response
键下。那么我该如何过滤这些。
希望您对此有所帮助。
【问题讨论】:
【参考方案1】:你试过responseArray(keyPath: "Games")
或responseArray(keyPath: "response.Games")
吗?因为您映射不知道如何解析所有字段。
【讨论】:
以上是关于如何使用对象映射器从响应 JSON 映射子数组?的主要内容,如果未能解决你的问题,请参考以下文章