使用 Alamofire 从 API 响应中获取空值
Getting null value from API response with Alamofire
我正在尝试使用 Alamofire 从 API url 获取响应值。我创建了一个数据模型并且响应很好,但我得到 poster_path
和 release_date
的空值。我想知道如何正确处理 JSON 响应。这是我的代码:
let decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
return decoder
}()
func fetchMovies( completion: @escaping(MovieResponse?, AFError?) -> Void) {
let url = URL(string: "url")!
let params = [:]
AF.request(url, method: .get , parameters: params)
.responseDecodable(of: Data.self, decoder: decoder) { response in
switch response.result {
case .success(let movies):
completion(movies, nil)
case .failure(let error):
completion(nil , error)
}
}
}
回复:
JSON 示例:
"results": [
{
"adult": false,
"backdrop_path": "/5hNcsnMkwU2LknLoru73c76el3z.jpg",
"genre_ids": [
35,
18,
10749
],
"id": 19404,
"original_language": "hi",
"original_title": "दिलवाले दुल्हनिया ले जायेंगे",
"overview": "Raj is a rich, carefree, happy-go-lucky second generation NRI. Simran is the daughter of Chaudhary Baldev Singh, who in spite of being an NRI is very strict about adherence to Indian values. Simran has left for India to be married to her childhood fiancé. Raj leaves for India with a mission at his hands, to claim his lady love under the noses of her whole family. Thus begins a saga.",
"popularity": 27.119,
"poster_path": "/2CAL2433ZeIihfX1Hb2139CX0pW.jpg",
"release_date": "1995-10-20",
"title": "Dilwale Dulhania Le Jayenge",
"video": false,
"vote_average": 8.7,
"vote_count": 3275
},
JSONDecoder
的keyDecodingStrategy
默认值为.useDefaultKeys
JSONDecoder
的dateDecodingStrategy
默认值为.deferredToDate
ps:
.deferredToDate
格式是 Apple 自己的日期格式,它跟踪自 2001 年 1 月 1 日以来的秒数和毫秒数。这不是很有用。
如您所写,posterPath
的 CodingKey
是 poster_path
所以JSONDecoder
的keyDecodingStrategy
,你不应该指定.convertFromSnakeCase
。
保留默认值。
对于JSON中的日期值,你应该自己写格式化程序
所以JSON解码器应该是:
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(formatter)
删除 Movie
中的整个 CodingKeys
枚举。
convertFromSnakeCase
策略已经进行了键映射。
并且不要粗心地将所有属性声明为可选。 themoviedb
等严肃服务发送非常一致的数据。至少很可能一部电影总是有一个标题和一个标识符。
我正在尝试使用 Alamofire 从 API url 获取响应值。我创建了一个数据模型并且响应很好,但我得到 poster_path
和 release_date
的空值。我想知道如何正确处理 JSON 响应。这是我的代码:
let decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
return decoder
}()
func fetchMovies( completion: @escaping(MovieResponse?, AFError?) -> Void) {
let url = URL(string: "url")!
let params = [:]
AF.request(url, method: .get , parameters: params)
.responseDecodable(of: Data.self, decoder: decoder) { response in
switch response.result {
case .success(let movies):
completion(movies, nil)
case .failure(let error):
completion(nil , error)
}
}
}
回复:
JSON 示例:
"results": [
{
"adult": false,
"backdrop_path": "/5hNcsnMkwU2LknLoru73c76el3z.jpg",
"genre_ids": [
35,
18,
10749
],
"id": 19404,
"original_language": "hi",
"original_title": "दिलवाले दुल्हनिया ले जायेंगे",
"overview": "Raj is a rich, carefree, happy-go-lucky second generation NRI. Simran is the daughter of Chaudhary Baldev Singh, who in spite of being an NRI is very strict about adherence to Indian values. Simran has left for India to be married to her childhood fiancé. Raj leaves for India with a mission at his hands, to claim his lady love under the noses of her whole family. Thus begins a saga.",
"popularity": 27.119,
"poster_path": "/2CAL2433ZeIihfX1Hb2139CX0pW.jpg",
"release_date": "1995-10-20",
"title": "Dilwale Dulhania Le Jayenge",
"video": false,
"vote_average": 8.7,
"vote_count": 3275
},
JSONDecoder
的keyDecodingStrategy
默认值为.useDefaultKeys
JSONDecoder
的dateDecodingStrategy
默认值为.deferredToDate
ps:
.deferredToDate
格式是 Apple 自己的日期格式,它跟踪自 2001 年 1 月 1 日以来的秒数和毫秒数。这不是很有用。
如您所写,posterPath
的 CodingKey
是 poster_path
所以JSONDecoder
的keyDecodingStrategy
,你不应该指定.convertFromSnakeCase
。
保留默认值。
对于JSON中的日期值,你应该自己写格式化程序
所以JSON解码器应该是:
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(formatter)
删除 Movie
中的整个 CodingKeys
枚举。
convertFromSnakeCase
策略已经进行了键映射。
并且不要粗心地将所有属性声明为可选。 themoviedb
等严肃服务发送非常一致的数据。至少很可能一部电影总是有一个标题和一个标识符。