小编Dmi*_*ry 的帖子

在 Swift 4 中解码小写和大写 JSON 键

我有以下表示 JSON 的结构:

struct Todo: Codable {
    let ID: Int?
    let LAST_DT_ADD: String?
    let LAST_ID:Int?
}
Run Code Online (Sandbox Code Playgroud)

当我以同样的方式使用解码时:

let decoder = JSONDecoder()
do {
  let todo = try decoder.decode(Todo.self, from: responseData)
  completionHandler(todo, nil)
} catch {
  print("error trying to convert data to JSON")
  print(error)
  completionHandler(nil, error)
}
Run Code Online (Sandbox Code Playgroud)

它可以正确解码,但是当我有小写的 JSON 项目时(例如,我有ID, LAST_DT_ADDand ,而不是, and ),它不会解码对象。我需要做什么?如何支持大写和小写?LAST_IDidlast_dt_addlast_id

swift swift4 jsondecoder

2
推荐指数
1
解决办法
4595
查看次数

标签 统计

jsondecoder ×1

swift ×1

swift4 ×1