gpt4 book ai didi

json - AlamofireObjectMapper 返回空 jsonarray

转载 作者:行者123 更新时间:2023-11-30 10:01:46 25 4
gpt4 key购买 nike

我的问题是:为什么我的方法返回一个带有 .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"]
}

}

最佳答案

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

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

.responseJSON { response in

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

响应序列化

内置响应方法

  • 响应()
  • responseData()
  • responseString(编码:NSStringEncoding)
  • responseJSON(选项:NSJSONReadingOptions)
  • responsePropertyList(选项:NSPropertyListReadOptions)
<小时/>
required convenience init?(_ map: Map) {
self.init()
}

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

关于json - AlamofireObjectMapper 返回空 jsonarray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38131048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com