json使用swift4解析struct

时间:2018-03-07 10:44:10

标签: json struct swift4 decodable

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 parName: String, method: Int, callBackID: String
  let pageID: String?
  let fields: [String: Properties]?, filter: [String: Properties]?, table: String?
  let imageid: String?, imagetable: String?, appTemplate: String?, appName: String?, values: Properties?, columns: [String: Properties]?, formID: String?
  let config: [String]? (sample only, this line is invalid.)
}

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

嗨!根据上面的数据,我的目标是打印" config"的值。我知道我可以通过创建更多的结构来实现这一点,但对很多人而言。也许这是一个简单的方法。

print(param.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"

我怎样才能实现这一目标^,感谢您的帮助:D

0 个答案:

没有答案