预期解码 Dictionary<String, Any> 但发现了字符串/数据

rio*_*ell 7 json struct swift4 decodable

我正在使用可文档协议,但遇到此错误:

typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [MakeApp_v2.Params.(CodingKeys in _244BBB2F32A8C5CF3DB84B0C6A94B232).config, Swift._DictionaryCodingKey(stringValue: "table", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found a string/data instead.", underlyingError: nil))
Run Code Online (Sandbox Code Playgroud)

这是我遇到错误的 JSON

{
  "callBackID" : "add1867f-6005-189c-bbb4-ff53202b0697",
  "config" : {
    "description" : "Welcome Page",
    "show-bottom-bar" : "",
    "css-page-align" : "",
    "footer" : { "show" : "", "object" : "bottom-bar" },
    "pageStyle" : "full-page",
    "sourceType" : "list",
    "title" : "Welcome Page",
    "header" : { "show" : true, "hide" : false, "object" : "top-bar" },
    "columns" : {},
    "objects" : {},
    "showtabs" : true,
    "slides" : [{"title" : "first section","map" : ["open"]}],
    "table" : "",
    "show-top-bar" : true,
    "style_max-width" : "",
    "slideType" : "slide"
  },
  "method" : 106,
  "parName" : "A1519614709427",
  "formid" : "1"
}
Run Code Online (Sandbox Code Playgroud)

我的结构

struct Params: Decodable {
  let callBackID: String, method: Int, parName: String, pageID: String?, table: String?, fields: [String: Properties]?, imageid: String?, imagetable: String?, config: [String: Config]?, appTemplate: String?, appName: String?, values: Properties?, columns: [String: Properties]?, filter: [String: Properties]?

  private enum CodingKeys: String, CodingKey {
    case callBackID = "callBackID", method = "method", parName = "parName", pageID = "pageID", table = "table", fields = "fields", imageid = "imageid", imagetable = "imagetable", config = "config", appTemplate = "appTemplate", appName = "appName", values = "values", columns = "columns", filter = "filter"
  }
}

struct Properties: Decodable {
  let source: String?, type: String?, rec_id: String?, name: String?, value: String?
}

struct Config: Decodable {
  let table: String?

  private enum CodingKeys: String, CodingKey {
    case table = "table"
  }
}
Run Code Online (Sandbox Code Playgroud)

获取 JSON 数据

var param: Params?
do {
    let jsonData = try JSONSerialization.data(withJSONObject: message.body, options: .prettyPrinted)
    print(String(data: jsonData, encoding: .utf8)!)
    param = try JSONDecoder().decode(Params.self, from: jsonData)
    print(param!)
} catch {
    print(error)
}
methods(param: param!)
print(methods)
}

func methods(param: Params) -> String { return "some string" }
Run Code Online (Sandbox Code Playgroud)

我有 3 组 JSON 数据结构,前两组使用此结构可以正常工作,但是上面的一组 JSON 数据使程序停止。我不确定要更新我的代码的内容。我希望你能帮我解决这个问题,TIA!

nay*_*yem 3

好吧,为了更清楚,我只是将我的评论转换为答案。

问题出在你的config: [String: Config]?内部Params结构。从 JSON 中可以明显看出,您的键config和值将是Config类型对象而不是Dictionary类型。因此将配置属性更改为config: Config?.

另请注意:当您的属性struct与 JSON 键相同时,您可以省略CodingKeys枚举(就像您对Properties结构所做的那样)

额外注意:optional我在你的结构定义中看到很多s 。请仅考虑您确实需要类型的情况optional。不要只是在不必要的时候使用它们。

struct Params: Decodable {
    let callBackID: String
    let method: Int
    let parName: String
    let pageID: String?
    let table: String?
    let fields: [String: Properties]?
    let imageid: String?
    let imagetable: String?
    let config: Config?
    let appTemplate: String?
    let appName: String?
    let values: Properties?
    let columns: [String: Properties]?
    let filter: [String: Properties]?
}
Run Code Online (Sandbox Code Playgroud)

对于进一步的说明,我更怀疑的是,您稍后也会遇到[String: Properties]?类型的麻烦。我怀疑这更像是这个config财产。