Fatal error: Dictionary<String, Any> does not conform to Decodable because Any does not conform to Decodable

Fatal error: Dictionary<String, Any> does not conform to Decodable because Any does not conform to Decodable

我正在尝试使用 swift 4 来解析本地 json 文件:

{
    "success": true,
    "lastId": null,
    "hasMore": false,
    "foundEndpoint": "https://endpoint",
    "error": null
}

这是我正在使用的函数:

    func loadLocalJSON() {

        if let path = Bundle.main.path(forResource: "localJSON", ofType: "json") {
            let url = URL(fileURLWithPath: path)

            do {
                let data  = try Data(contentsOf: url)
                let colors = try JSONDecoder().decode([String: Any].self, from: data)
                print(colors)
            }
            catch { print("Local JSON not loaded")}
        }
    }
}

但我一直收到错误消息:

Fatal error: Dictionary does not conform to Decodable because Any does not conform to Decodable.

我尝试在这个 Whosebug 页面上使用 "AnyDecodable" 方法:How to decode a 属性 with type of JSON dictionary in Swift 4可解码协议 但它跳转到 'catch' 语句: catch { print("Local JSON not loaded") 使用时。有谁知道如何在 Swift 4 中解析此 JSON 数据?

也许您误解了 Codable 的工作原理。它基于具体类型。 Any 不支持。

在你的情况下,你可以创建一个像

这样的结构
struct Something: Decodable {
    let success : Bool
    let lastId : Int?
    let hasMore: Bool
    let foundEndpoint: URL
    let error: String?
}

并解码 JSON

func loadLocalJSON() {
    let url = Bundle.main.url(forResource: "localJSON", withExtension: "json")!
    let data  = try! Data(contentsOf: url)
    let colors = try! JSONDecoder().decode(Something.self, from: data)
    print(colors)
}

任何崩溃都会暴露设计错误。在主包中的文件中使用 null 的意义是另一个问题。

我使用 quicktype 生成 Codable 和编组代码:

https://app.quicktype.io?gist=02c8b82add3ced7bb419f01d3a94019f&l=swift

我根据你的样本数据给了它一组样本:

[
  {
    "success": true,
    "lastId": null,
    "hasMore": false,
    "foundEndpoint": "https://endpoint",
    "error": null
  },
  {
    "success": true,
    "lastId": 123,
    "hasMore": false,
    "foundEndpoint": "https://endpoint",
    "error": "some error"
  }
]

这告诉 quicktype 假设第一个示例中的 null 值有时是 IntString – 如果它们不是可能的类型,您可以更改它们。生成的 Codable 是:

struct Local: Codable {
    let success: Bool
    let lastID: Int?
    let hasMore: Bool
    let foundEndpoint: String
    let error: String?

    enum CodingKeys: String, CodingKey {
        case success
        case lastID = "lastId"
        case hasMore, foundEndpoint, error
    }
}