Swift JSON 序列化类型不匹配

Mar*_*ark 3 serialization json struct swift decodable

目前正在努力如何使用 Decodable。我已经用谷歌搜索了我遇到的错误,但我仍然相信我构建结构的方式不正确,但对我来说似乎有意义。

\n\n

我也尝试过使用选项

\n\n

在我最后发布的错误中,我对 Double 类型的引用感到困惑。因为我没有任何类型或任何使用双精度的响应。

\n\n

(我还可以使用将数据转换为字典的旧 swift 方法来序列化 json 响应 - [String : Any]。但我想使用现代/更新的方法。)

\n\n

JSON 响应

\n\n
    {"NEWS":\n  [\n    {\n      "DATE":"2018-10-13T03:56:06+1000",\n      "SOURCE":"smh.com.au",\n      "BLURB":"Assistant Treasurer Stuart Robert says he has repaid $37,975 of \\"excess usage charges\\" in home internet bills footed by taxpayers.",\n      "ID":102347,\n      "TITLE":"Stuart Robert pays back $38,000 in excessive home internet charges"\n    },\n    {\n      "DATE":"2018-10-12T18:00:38+1000",\n      "SOURCE":"itwire.com",\n      "BLURB":"The CVC costs set by the NBN Co make it very difficult for ISPs to offer gigabit connections to more than a select band of customers who are willing to sign up in numbers and pay slightly more than other speed tiers, according to one ISP who caters to this type of consumer.",\n      "ID":102343,\n      "TITLE":"NBN gigabit connections will remain mostly a pipe dream"},\n    {\n      "DATE":"2018-10-12T09:48:43+1000",\n      "SOURCE":"computerworld.com.au",\n      "BLURB":"The Department of Home Affairs has rejects calls to include independent judicial oversight of the decision to issue Technical Assistance Notices and Technical Capability Notices as part of proposed legislation intended to tackle police agencies\xe2\x80\x99 inability to access encrypted communications services.",\n      "ID":102342,\n      "TITLE":"Home Affairs rejects calls for additional safeguards in \xe2\x80\x98spyware\xe2\x80\x99 law"\n    },\n    {\n    "DATE":"2018-10-11T12:16:05+1000",\n    "SOURCE":"itnews.com.au",\n    "BLURB":"NBN Co is hoping to \xe2\x80\x9cspeed up\xe2\x80\x9d building works on the fibre-to-the-curb (FTTC) portion of its network as it tries to make up lost ground.",\n    "ID":102334,\n    "TITLE":"NBN Co says fibre-to-the-curb build is more complex that it hoped"\n    },\n  ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码

\n\n
struct Root: Decodable {\n    let news: [News]?\n\n    enum CodingKeys: String, CodingKey {\n        case news = "NEWS"\n    }\n\n}\n\nstruct News: Decodable {\n    let date: Date\n    let source, blurb: String\n    let id: Int\n    let title: String\n\n    enum CodingKeys: String, CodingKey {\n        case date = "DATE"\n        case source = "SOURCE"\n        case blurb = "BLURB"\n        case id = "ID"\n        case title = "TITLE"\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

序列化

\n\n
URLSession.shared.dataTask(with: url) { (data, response, err) in  \nguard let dataStr = data else {\n            return\n        }\ndo {\n    let root = try JSONDecoder().decode(Root.self, from: dataStr) //error is caught here\n    guard let r = root else { return }\n    print(r.news)\n} catch let err {\n    print("JSON Error - \\(err)")\n}\n}.resume()\n
Run Code Online (Sandbox Code Playgroud)\n\n

错误

\n\n
\n

序列化json时出错 typeMismatch(Swift.Double, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "NEWS", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: " DATE", intValue: nil)],debugDescription: "预期解码 Double,但发现了字符串/数据。",underlyingError: nil))

\n
\n

Kon*_*Kon 6

这是因为 Date 的默认编码策略是 double(自纪元以来的秒数)。您可以将默认策略更改为 iso8061 或任何自定义策略。例如,您可以在解码器中设置日期格式化程序,如下所示:

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
Run Code Online (Sandbox Code Playgroud)