预计解码字典 但发现了一个字符串/数据

问题描述 投票:0回答:2

我正在使用docodable协议,我遇到了这个错误:

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))

这是我遇到错误的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"
}

我的结构

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"
  }
}

获取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" }

我有3套JSON数据结构,前两组工作正常,但上面的一个JSON数据使程序停止。我不知道我的代码会更新什么。我希望你能帮我解决这个问题,TIA!

json struct swift4 decodable
2个回答
1
投票

好吧,我只是将我的评论转换成答案,以便更清晰。

问题是你在config: [String: Config]?结构内的Params。从JSON中可以清楚地看出,你的密钥是config,值是Config类型的对象,而不是Dictionary类型。所以将config属性更改为config: Config?

另一个注意事项:当你的struct中的属性与JSON键相同时,你可以省略CodingKeys枚举(就像你使用Properties结构一样)

额外注意:我在你的结构定义中看到了很多optionals。请仅考虑您真正需要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]?
}

对于进一步的说明,我怀疑更多的是,你也会在以后的[String: Properties]?类型中遇到麻烦。它更像是我怀疑的config财产。


0
投票
struct Params: Decodable {
  let callBackID: String, method: Int, parName: String, pageID: String?, table: String?, fields: [String: Properties]?, imageid: String?, imagetable: String?, config: 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"
  }
}

对不起这是我的错!我的密钥配置错误的对象类型。而是config = [String:Config?]更改为config = Config?归功于@nayem

© www.soinside.com 2019 - 2024. All rights reserved.