SWIFT: 尝试解码 JSON ,它 returns nil
SWIFT: Trying to decode JSON ,it returns nil
当你尝试调用 .decode() 来解码结构时,它 returns nil.Can 有人帮助我吗?
我的应用从 JSON 数据返回空值,从这行代码:
let newPosData = try JSONDecoder().decode(NewPosDataBase.self, from: responseData)
完整代码如下:
func getNewLFNREXT(invnr: String, completionHandler: @escaping (String, Bool, NewPosDataBase?) -> Void) {
let headers: HTTPHeaders = [
.authorization(username: UserCredentials.shared.username, password: UserCredentials.shared.password),
.accept("application/json")
]
let url:String = Config.middleware + Config.url + "/mobile/lfnrext?invnr=\(invnr)"
AF.request(url, method: .get, parameters: [:], headers: headers).responseJSON { response in
if response.response?.statusCode == 200 {
do {
guard let responseData = response.data else {
completionHandler(errorMessage, false, nil )
return
}
let newPosData = try JSONDecoder().decode(NewPosDataBase.self, from: responseData)
print( newPosData.newPosData?.newLfnrext)
completionHandler("", true, newPosData)
} catch {
completionHandler(errorMessage, false , nil )
}
}else {
let message = self.getErrorMessageFrom(data: response.data, defaultErrorMessage: errorKeineDaten)
completionHandler(message, false, nil)
}
}
}
如果键的名称与 Codable
类型的 属性 名称不同,您需要添加 enum CodingKeys
。
所以,你的模型应该是这样的,
class NewPosDataBase: Codable {
enum CodingKeys: String, CodingKey {
case newPosData = "NEW_POS_DATA"
}
//rest of the code...
}
class NewPosData: Codable {
enum CodingKeys: String, CodingKey {
case newLfnrext = "NEW_LFNREXT"
case aktuellerZahler = "AKTUELLER_ZAHLER"
case gpsStd = "GPS_STD"
}
//rest of the code...
}
注意:无需在模型中提供任何 init
实现,除非您出于任何特定目的需要它们。
当你尝试调用 .decode() 来解码结构时,它 returns nil.Can 有人帮助我吗?
我的应用从 JSON 数据返回空值,从这行代码:
let newPosData = try JSONDecoder().decode(NewPosDataBase.self, from: responseData)
完整代码如下:
func getNewLFNREXT(invnr: String, completionHandler: @escaping (String, Bool, NewPosDataBase?) -> Void) {
let headers: HTTPHeaders = [
.authorization(username: UserCredentials.shared.username, password: UserCredentials.shared.password),
.accept("application/json")
]
let url:String = Config.middleware + Config.url + "/mobile/lfnrext?invnr=\(invnr)"
AF.request(url, method: .get, parameters: [:], headers: headers).responseJSON { response in
if response.response?.statusCode == 200 {
do {
guard let responseData = response.data else {
completionHandler(errorMessage, false, nil )
return
}
let newPosData = try JSONDecoder().decode(NewPosDataBase.self, from: responseData)
print( newPosData.newPosData?.newLfnrext)
completionHandler("", true, newPosData)
} catch {
completionHandler(errorMessage, false , nil )
}
}else {
let message = self.getErrorMessageFrom(data: response.data, defaultErrorMessage: errorKeineDaten)
completionHandler(message, false, nil)
}
}
}
如果键的名称与 Codable
类型的 属性 名称不同,您需要添加 enum CodingKeys
。
所以,你的模型应该是这样的,
class NewPosDataBase: Codable {
enum CodingKeys: String, CodingKey {
case newPosData = "NEW_POS_DATA"
}
//rest of the code...
}
class NewPosData: Codable {
enum CodingKeys: String, CodingKey {
case newLfnrext = "NEW_LFNREXT"
case aktuellerZahler = "AKTUELLER_ZAHLER"
case gpsStd = "GPS_STD"
}
//rest of the code...
}
注意:无需在模型中提供任何 init
实现,除非您出于任何特定目的需要它们。