ios - 带有嵌套数据的 Swift 4 JSON 解码器

标签 ios json swift jsondecoder

这是数据结构(已更新):

    {
  "date": "2018-10-18",
  "time_of_day": "16:00",
  "request_time": "2018-10-18T16:00:27+01:00",
  "station_name": "London Waterloo",
  "station_code": "WAT",
  "arrivals": {
    "all": [
      {
        "mode": "train",
        "service": "24673605",
        "train_uid": "W12378",
        "platform": "10",
        "operator": "SW",
        "operator_name": "South Western Railway",
        "aimed_departure_time": null,
        "aimed_arrival_time": "05:18",
        "aimed_pass_time": null,
        "origin_name": "Guildford",
        "destination_name": "London Waterloo",
        "source": "ATOC",
        "category": "OO",
        "service_timetable": {
          "id": "https://transportapi.com/v3/uk/train/service/train_uid:W12378/2018-10-18/timetable.json?app_id=80b56d0a&app_key=b44a5870830959a7a961fdbb65f9dc13"
        }
      },
      {
        "mode": "train",
        "service": "24671505",
        "train_uid": "W14110",
        "platform": "1",
        "operator": "SW",
        "operator_name": "South Western Railway",
        "aimed_departure_time": null,
        "aimed_arrival_time": "05:35",
        "aimed_pass_time": null,
        "origin_name": "Twickenham",
        "destination_name": "London Waterloo",
        "source": "ATOC",
        "category": "OO",
        "service_timetable": {
          "id": "https://transportapi.com/v3/uk/train/service/train_uid:W14110/2018-10-18/timetable.json?app_id=80b56d0a&app_key=b44a5870830959a7a961fdbb65f9dc13"
        }
      },
      {
        "mode": "train",
        "service": "24671105",
        "train_uid": "W14764",
        "platform": "15",
        "operator": "SW",
        "operator_name": "South Western Railway",
        "aimed_departure_time": null,
        "aimed_arrival_time": "05:41",
        "aimed_pass_time": null,
        "origin_name": "Staines",
        "destination_name": "London Waterloo",
        "source": "ATOC",
        "category": "OO",
        "service_timetable": {
          "id": "https://transportapi.com/v3/uk/train/service/train_uid:W14764/2018-10-18/timetable.json?app_id=80b56d0a&app_key=b44a5870830959a7a961fdbb65f9dc13"
        }
      }
    ]
  }
}

我在这里试过答案:Expected to decode Array<Any> but found a dictionary instead

但不能完全让它工作。我一直在这条线上收到错误消息:

let root = try JSONDecoder().decode(Root.self, from: data)

我的模型(更新):

 struct Root: Decodable {
    let arrivals: Arrivals
}

struct Arrivals: Decodable {
    let all: All
}

struct All: Decodable {
    let trains: [Train]
}

错误:

▿ DecodingError
  ▿ typeMismatch : 2 elements
    - .0 : Swift.Dictionary<Swift.String, Any>
    ▿ .1 : Context
      ▿ codingPath : 2 elements
        - 0 : CodingKeys(stringValue: "arrivals", intValue: nil)
        - 1 : CodingKeys(stringValue: "all", intValue: nil)
      - debugDescription : "Expected to decode Dictionary<String, Any> but found an array instead."
      - underlyingError : nil

最佳答案

"I've tried the answer here: Expected to decode Array but found a dictionary instead

是的,但你的错误信息恰恰相反。花点时间阅读错误消息,我们可以看到它非常清楚并且显然是正确的。

在您的 Arrivals 结构中,您说的是

let all: All

但是JSON中"all"键的值是一个数组!所以这里的类型一定是某种东西的数组。特别是它应该是一系列火车。

let all: [Train]

现在你可以删除你的 All 结构,这是不对的。

示例(针对您显示的 JSON 运行):

struct Root: Decodable {
    let arrivals: Arrivals
}
struct Arrivals: Decodable {
    let all: [Train]
}
struct Train: Decodable {
}
let data = json.data(using: .utf8)!
if let root = try? JSONDecoder().decode(Root.self, from: data) {
    print("got", String(root.arrivals.all.count), "trains")
}
// "got 3 trains"

关于ios - 带有嵌套数据的 Swift 4 JSON 解码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52876103/

相关文章:

ios - 带有子 MVVM 的 MVVM 在 iOS 中如何工作?

python - 字符串是 Python 中的 JSON 对象还是 JSON 数组?

iOS Swift3 TextView 行在错误的位置

javascript - AppCache 在 iOS 上非常慢

ios - 为什么 viewDidLayout 在 iOS 7 上只被调用一次,但在 iOS 8 上被调用多次

ios - 如果当前部分单元格高度为零,则索引路径处的行的 Collectionview 单元格不会被后续部分调用

Python SUDS 返回类型不是 XML

java - Gson 和数组

cocoa-touch - 在 swift xcode 中初始化

ios - 在没有 Storyboard 的情况下呈现 ViewController Segue 过渡 [Swift]