如何使用对象映射器从响应JSON映射子数组?

时间:2016-12-26 11:05:22

标签: ios swift3 nsarray alamofire objectmapper

我正在使用AlamofireAlamofire Object Mapper

认为这是来自Web服务的响应:

{
  "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 个答案:

答案 0 :(得分:0)

您是否尝试过responseArray(keyPath: "Games")responseArray(keyPath: "response.Games")?因为您映射不知道如何解析所有字段。