json - 未加密的容器 JSON Swift 可解码

标签 json swift decodable

我正在尝试解码一些 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 的新手,所以我有点困惑我做错了什么? 有谁知道我该如何解决我的问题? 现在我收到警告,它需要一个数组,但它正在查找字典。因此,我认为我已经成功通过了第一个未加密的容器,但我无法进入其余容器。

最佳答案

删除你的初始化方法然后做

let decoder = JSONDecoder()

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

关于json - 未加密的容器 JSON Swift 可解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56328803/

相关文章:

python - DJANGO - 文件未找到错误 : [Errno 2] No such file or directory: '' when trying to acess a file

c# - JSON.net 直接从 oledbconnection 序列化

ios - 使用 Swift 通过 PHP 将图像上传到我的服务器

swift - 两个 ViewControllers Swift 中的 NSNotification addObserver

ios - 需要从父类(super class)属性访问子类属性

android - HashMap 数组列表到 Android 中的 JSON 数组?

json - 为什么在解码 JSON 时字符串和 []bytes 的处理方式不同?

swift - NSView 的 mouseMoved 永远不会被调用

ios - 解码为 NSManagedObject 对象后需要获取请求

ios - Swift 解码具有多种不同格式的数据类型