【问题标题】:Unkeyed Container JSON Swift Decodable无键容器 JSON Swift 可解码
【发布时间】:2019-05-27 15:23:28
【问题描述】:

我正在尝试解码一些 JSON。这是 JSON 的示例:

 [
    {
        "type": "departure",
        "status": "landed",
        "departure": {
            "iataCode": "JFK",
            "icaoCode": "KJFK",
            "scheduledTime": "2017-12-11T01:06:00.000",
            "estimatedRunway": "2017-12-11T02:07:00.000",
            "actualRunway": "2017-12-11T02:07:00.000" },
        "arrival": {
            "iataCode": "CVG",
            "icaoCode": "KCVG",
            "estimatedRunway": "2017-12-11T03:38:00.000",
            "actualRunway": "2017-12-11T03:38:00.000"
        },
        "airline": {
            "name": "Atlas Air",
            "iataCode": "5Y",
            "icaoCode": "GTI"
        },
        "flight": {
            "number": "302",
            "iataNumber": "5Y302",
            "icaoNumber": "GTI302"
        }
    }, 
    {

        //Same keys as above.
    }, 
    //Etc.
]

它以无键数组开始。紧随其后的是同样未加密的 JSON 容器。我无法使用此代码将其拆分:

struct Dataset: Decodable {

    var data: [FlightData]

    init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        print(container)
        data = [try container.decode(FlightData.self)]
    }

struct FlightData: Decodable {

    var type: String //Arrival or Departure
    var status: String //Flight Status

    var departure: Departure
    var arrival: Arrival
    var airline: Airline
    var flight: Flight

    struct Departure: Decodable {

        var iataCode: String
        var icaoCode: String
        var terminal: String
        var gate: String
        var scheduledTime: String
        var estimatedTime: String
        var actualTime: String
        var estimatedRunway: String
        var actualRunway: String

    }

    struct Arrival: Decodable {

        var iataCode: String
        var icaoCode: String
        var terminal: String
        var gate: String
        var baggage: String
        var scheduledTime: String
        var estimatedTime: String
        var actualTime: String
        var estimatedRunway: String
        var actualRunway: String

    }

    struct Airline: Decodable {

        var name: String
        var iataCode: String
        var icaoCode: String

    }

    struct Flight: Decodable {

        var number: String
        var iataNumber: String
        var icaoNumber: String

    }

}
}

我是 JSON 和 Swift Decodable 的新手,所以我有点困惑我做错了什么? 有谁知道我可以如何解决我的问题? 现在我收到警告说它需要一个数组,但它正在查找字典。因此,我认为我已经成功通过了第一个未锁定的容器,但我无法进入其中的其余部分。

【问题讨论】:

  • 随后是同样未加密的 JSON 容器。那是错误的。所有其他集合类型都是(键控)字典。解码 [FlightData].self 而不是 Dataset.self
  • 数组中的集合没有键控。请参阅我的编辑。等等。我的意思是它是一个未键入对象的数组,其中包含显示的字典。 @vadian
  • 给定 JSON 中数组内的集合都是字典。如果Etc 中有其他数组,为什么不发布它们而不是实际上完全不相关的 JSON?
  • 你误会了我......感谢完全不相关的cmets。 Joakim Danielson 给了我想要的答案。 @vadian

标签: json swift decodable


【解决方案1】:

删除你的init方法然后做

let decoder = JSONDecoder()

do {
    data = try decoder.decode([FlightData].self, from: data) 
} catch {
    print(error)
}

【讨论】:

    猜你喜欢
    • 2018-07-02
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    相关资源
    最近更新 更多