gpt4 book ai didi

ios - Codable 错误 - 预期解码 Dictionary 但找到数组

转载 作者:行者123 更新时间:2023-11-28 23:28:26 29 4
gpt4 key购买 nike

请有人帮我指出我做错了什么。我搜索过其他类似的问题,但似乎没有一个有助于解决这个问题我正在尝试使用 Swift Codable 解码 json

我已经创建了嵌套的可编码结构,但它一直失败。

可以在此处找到正在尝试解码的 json - https://pastebin.com/C0QCREEF

这是失败的可编码结构 -

struct OutboundFlightData: Codable {
var data: OutboundFlight
}
struct OutboundFlight: Codable {
var outboundFlights: [OutboundFlightDetails]
}
struct OutboundFlightDetails: Codable {
var amount: String
var fareFamily: Bool
var cabin: String
var fareBasis: String
var duration: String
var itinerary: [OutboundFlightItinerary]
}
struct OutboundFlightItinerary: Codable {
var timeOfDeparture: String
var timeOfArrival: String
var dateOfDeparture: String
var dateOfArrival: String
var departureLocation: String
var departureDetails: FlightItineraryDetails
var arrivalLocation: String
var arrivalDetails: [FlightItineraryDetails]
var departureTerminal: String?
var arrivalTerminal: String?
var operatingCompany: String
var operatingDetails: FlightItineraryOperatingDetails
var marketingCompany: String
var marketingDetails: FlightItineraryOperatingDetails
var flightNumber: String
var electronicTicketing: String
}
struct FlightItineraryOperatingDetails: Codable {
var iata: String
var fullName: String
var countryName: String
var logoSmall: String
var logo: String
enum CodingKeys: String, CodingKey {
case iata
case fullName = "full_name"
case logoSmall = "logo_small"
case logo
case countryName = "country_name"
}
}
struct FlightItineraryDetails: Codable {
var iata: String
var name: String
var cityName: String
var stateName: String
var countryName: String
enum CodingKeys: String, CodingKey {
case iata
case name
case cityName = "city_name"
case stateName = "state_name"
case countryName = "country_name"
}
}

我希望 json 能够成功解码。但是我不断收到错误 -

typeMismatch(
Swift.Array<Any>,
Swift.DecodingError.Context(
codingPath: [
CodingKeys(stringValue: "data", intValue: nil),
CodingKeys(stringValue: "outboundFlights", intValue: nil),
_JSONKey(stringValue: "Index 0", intValue: 0),
CodingKeys(stringValue: "itinerary", intValue: nil),
_JSONKey(stringValue: "Index 0", intValue: 0),
CodingKeys(stringValue: "arrivalDetails", intValue: nil)
],
debugDescription: "Expected to decode Array<Any> but found a dictionary instead.",
underlyingError: nil
)
)

但是当我从数组中更改 arrivalDetails 时。我收到此错误

typeMismatch(
Swift.Dictionary<Swift.String, Any>,
Swift.DecodingError.Context(
codingPath: [
CodingKeys(stringValue: "data", intValue: nil),
CodingKeys(stringValue: "outboundFlights", intValue: nil),
_JSONKey(stringValue: "Index 6", intValue: 6),
CodingKeys(stringValue: "itinerary", intValue: nil),
_JSONKey(stringValue: "Index 1", intValue: 1),
CodingKeys(stringValue: "arrivalDetails", intValue: nil)
],
debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.",
underlyingError: nil
)
)

最佳答案

从您提供的示例 JSON 来看,arrivalDetails 字段似乎是用字典或空数组填充的。这就是为什么当您在 [FlightItineraryDetails]FlightItineraryDetails 之间切换时,无论哪种方式都会出错,因为解码器无法将字典解析为数组,或将数组解析为数组词典。

不幸的是,Codable 无法开箱即用地处理这种情况。您需要为这种情况创建一个枚举,尝试以两种方式解析该键,返回它在数据中找到的任何一个。

(此代码无耻地从将 JSON 馈送到 https://app.quicktype.io 的输出中提取出来。我总是建议使用它来将 JSON 示例从 API 映射到 Codable 结构。它至少让您完成了 80% 的工作,并处理像这样的奇怪情况,其中 API 表现不佳。)

enum ArrivalDetails: Codable {
case anythingArray([JSONAny])
case details(Details)

init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode([JSONAny].self) {
self = .anythingArray(x)
return
}
if let x = try? container.decode(Details.self) {
self = .details(x)
return
}
throw DecodingError.typeMismatch(ArrivalDetails.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for ArrivalDetails"))
}

func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .anythingArray(let x):
try container.encode(x)
case .details(let x):
try container.encode(x)
}
}
}

如果您可以控制返回此结果的 API,您可以考虑将该字段更改为不存在,或者如果该字段应该为空而不是空数组则设置为 null。然后,您只需将其改回 [FlightItineraryDetails]?

关于ios - Codable 错误 - 预期解码 Dictionary <String, Any> 但找到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57779342/

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