AlamofireObjectMapper返回空的jsonarray

时间:2016-06-30 18:47:50

标签: json swift alamofire objectmapper

我的问题是:为什么我的方法返回带有.responseArray的空json数组?

我有来自REST-API的以下JSON:

[
   {
       "id": 1,
       "title": "Crazy",
       "startDate": "2016-07-16 22:00:00.0",
       "endDate": "2016-07-17 03:00:00.0",
       "location": "Crazy",
       "description": "Crazy",
       "avatar": "base64"
    }, 
    {
        "id": 2,
        "title": "Crazy title",
        "startDate": "2016-07-16 22:00:00.0",
        "endDate": "2016-07-17 03:00:00.0",
        "location": "Crazy",
        "description": "Something",
        "avatar": "base64"
    },
    {
        "id": 3,
        "title": "Crazy",
        "startDate": "2016-07-16 22:00:00.0",
        "endDate": "2016-07-17 03:00:00.0",
        "location": "Crazy",
        "description": "Crazy",
        "avatar": "base64"
    }, 
    {
        "id": 4,
        "title": "Crazy",
        "startDate": "2016-07-16 22:00:00.0",
        "endDate": "2016-07-17 03:00:00.0",
        "location": "Crazy",
        "description": "Crazy",
        "avatar": "base64"
}]

json是一个包含多个对象的数组,如您所见。

我使用AlamofireObjectMapper将json映射为一个对象,但是它给我一个空数组:

 func getEventFromApiWebService(completionHandler: (event: [Event]) -> ()){
    Alamofire.request(ApiRouter.Get("/events"))
        .validate()
        .responseArray(completionHandler: { (response: Response<[Event], NSError>) in

            //Attempt to print result value which returns empty Array
            let eventArray = response.result.value
            print(eventArray!)

            completionHandler(event: response.result.value!)

        })
}

以下代码适用于.responseJSON:

    func test(){
    Alamofire.request(ApiRouter.Get("/events"))
        .responseJSON { response  in

            print(response.result.value!)
    }
}

但是这里没有映射。

事件类:

import ObjectMapper
import RealmSwift

class Event: Object, Mappable {

    dynamic var id = 0
    dynamic var eventID = 0
    dynamic var title: String? = nil
    dynamic var startDate: NSDate? = nil
    dynamic var endDate: NSDate? = nil
    dynamic var location: String? = nil
    dynamic var desc: String? = nil
    dynamic var avatar: NSData? = nil

    override static func primaryKey() -> String? {
        return "id"
    }

    required convenience init?(_ map: Map) {
        self.init()

        if map["id"].value() == nil {
            return nil
        }
        if map["title"].value() == nil {
            title = ""
        }
        if map["startDate"].value() == nil {
            startDate = nil
        }
        if map["endDate"].value() == nil {
            endDate = nil
        }
        if map["location"].value() == nil {
            location = ""
        }
        if map["description"].value() == nil {
            desc = ""
        }
        if map["avatar"].value() == nil {
            avatar = nil
        }
    }

    func mapping(map: Map) {
        self.eventID        <- map["id"]
        self.title          <- map["title"]
        self.startDate      <- map["startDate"]
        self.endDate        <- map["endDate"]
        self.location       <- map["location"]
        self.desc           <- map["description"]
        self.avatar         <- map["avatar"]
    }

} 

2 个答案:

答案 0 :(得分:1)

Alamofire没有responseArray方法。 https://github.com/Alamofire/Alamofire#response-serialization

使用responseJSON获取JSON并转换为数组。

.responseJSON { response in

    guard response.result.isSuccess else
    {
          //handle error 
          return
    }

响应序列化

内置响应方法

  • 响应()
  • responseData()
  • responseString(encoding:NSStringEncoding)
  • responseJSON(选项:NSJSONReadingOptions)
  • responsePropertyList(options:NSPropertyListReadOptions)
required convenience init?(_ map: Map) {
    self.init()
}

func mapping(map: Map) {
    id    <- map["id"]
    title <- map["title"]
    startDate <- map["startDate"]
    endDate <- map["endDate"]
}

答案 1 :(得分:0)

事实证明我的事件类中的DataTypes与JSON没有关系。 在required convenience init?(_ map: Map)的某个地方它给了我一个零,因此拒绝反序列化整个JsonArray。