Json 阿拉莫菲尔得到回应
我在Json 阿拉莫菲尔得到回应,json,swift,get,alamofire,Json,Swift,Get,Alamofire,我在let response=try中得到一个零?JSONDecoder().decode(选择courseresponse.self,from:data)有什么原因吗 func getMotherLangRequest() { showActivityIndicatory(uiView: view, show: true) AF.request(NetworkUrl.motherLang_getRequest, method: .get)
let response=try中得到一个零?JSONDecoder().decode(选择courseresponse.self,from:data)
有什么原因吗
func getMotherLangRequest() {
showActivityIndicatory(uiView: view, show: true)
AF.request(NetworkUrl.motherLang_getRequest,
method: .get)
.responseJSON { response in
self.showActivityIndicatory(uiView: self.view, show: false)
debugPrint(response)
switch response.result {
case .success:
guard let data = response.data else { return }
let restResponse = try? JSONDecoder().decode(SelectCourseResponse.self, from: data)
if restResponse?.status == 0 {
} else { self.showErrorResponse(data: data) }
case let .failure(error): print(error)
}
}
}
这是我的请求
我的SelectCourseResponse结构
我建议使用do/catch块并打印出错误。也就是说,JSON对象中有许多空字段,但在数据结构中没有将这些字段定义为可选字段<代码>更新日期、
价格
和图标
都应该是可选的
do {
let restResponse = try JSONDecoder().decode(
SelectCourseResponse.self,
from: data
)
process(restReponse)
} catch {
print("DECODE ERROR: \(error)")
}
您的价格、图标和更新日期字段为空,请将它们声明为可选字段,您的代码将正常工作 您是否可以添加
SelectCourseResponse
struct
?您应该使用responseDecodable
,它将产生一个错误,您可以查看解析失败的原因。