json序列化错误keyNotFound(CodingKeys..)-当我使用Wi-Fi或VPN连接时



我有一个从服务器获取数据的函数,但有时当应用程序确实通过Wifi或VPN请求时,我获得。当应用程序通过wifi或VPN请求时,为什么会发生这种情况?

错误:

Error json serialization keyNotFound(CodingKeys(stringValue: "host", intValue: nil),
Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "recommendations", intValue:
nil), _JSONKey(stringValue: "Index 2", intValue: 2), CodingKeys(stringValue: "blockedHosts",
intValue: nil), _JSONKey(stringValue: "Index 1", intValue: 1)], debugDescription: "No value 
associated with key CodingKeys(stringValue: "host", intValue: nil) ("host").", 
underlyingError: nil))

模型:

class ListOfPlayers: Decodable {
let players: [Player]
let recommendations: [Recommendations]
let proxies: [Proxies]
let preloadAdStartPageUrl: String
}
class Recommendations: Decodable {
let id: Int
let url: String
let title: String
let color: String
let image: String?
let blockedHosts: [BlockedHostsRecommendation]
}
class BlockedHostsRecommendation: Decodable {
let host: String
let countries: [String]
}

我重复一遍,应用程序运行良好,但有时我只能通过Wifi或VPN捕获此错误。

我建议使用Charles这样的代理捕获网络流量并分析接收到的数据。错误信息显示JSONDecoder不能解码BlockedHostsRecommendation.host,在某些情况下,您的后端可能没有提供该字段

最新更新