Swift4 的 JSON 解码器

标签 json struct decode swift4 codable

使用Swift4、iOS11.1、Xcode9.1,

尝试匹配 Swift4 的可编码结构中的 JSON 文件,我遇到以下问题:

enter image description here

这是我的代码:

struct Station: Codable {

    let htmlAttributions: [String]
    let nextPageToken: String
    let status: String

    struct Result: Codable {
        let id: String

        enum CodingKeys : String, CodingKey {
            case id
        }
    }

    enum CodingKeys : String, CodingKey {
        case htmlAttributions = "html_attributions"
        case nextPageToken = "next_page_token"
        case status
        case result
    }
}

据我了解,我必须放置枚举才能允许 JSON 文件中出现下划线键。但是添加 Enum 后,会出现错误消息。我的枚举有什么问题?如何正确放置?

你能让我尝试一下以下 JSON 文件吗?我必须如何在结构中匹配它?任何帮助表示赞赏!

这是 JSON 文件:

{
   "html_attributions" : [],
   "next_page_token" : "F3ddaOzOcyo94AA2skDm",
   "results" : [
      {
         "formatted_address" : "Strasse 1, 6003 Luzern, Switzerland",
         "geometry" : {
            "location" : {
               "lat" : 47.04951260000001,
               "lng" : 8.310404999999999
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 47.0508615802915,
                  "lng" : 8.311753980291503
               },
               "southwest" : {
                  "lat" : 47.0481636197085,
                  "lng" : 8.309056019708498
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "a3d600a6e78105b6ce2b5f5a3fac98ca1910a09b",
         "name" : "Luzern",
         "photos" : [
            {
               "height" : 4000,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/113951418385089253589/photos\"\u003eAlex Marcu\u003c/a\u003e"
               ],
               "photo_reference" : "CmRaAAAAYHK1VHDFlkbzXuMnF2MLEdew-36lgHC2lS1Cxg_DELgP-ckZH7G6aa-81LGDpR5rPZY1XMw64mytsjXIrdB5n3QQmXjGgelwZEbHaetT2jpy9SeaHDH3qUGGAUW-7BtZEhCxXy2dxGSv6A_g7fipsCr5GhRZlPuliykokXIkqfqIN_vMWzmYyA",
               "width" : 3000
            }
         ],
         "place_id" : "ChIJqQIbhpj7j0cRjUguIM__gZw",
         "rating" : 4.4,
         "reference" : "CmRSAAAAzBZCshpypXcbMhrBQIdK2zISd3Q40QRSFO0KKhIrTejnGiZIoASuVqCVtmNBnFsodLWrYtOP-RmwCqDBDVbMheeCbFk7f0L8gwixLx_SGhYTDqPd6B2IwPWWXH5Pb6lxEhBoQtWj-kB-g1ZiOZ74hswNGhSd9Kf9Qj1P2_fdQCTO_VCoTU09JA",
         "types" : [
            "transit_station",
            "bus_station",
            "train_station",
            "point_of_interest",
            "establishment"
         ]
      },
      { ...more results... },
      { ...more results... }
   ],
   "status" : "OK"
}

有人可以帮我解决这个问题吗?

最佳答案

你的struct Station是这样正确的吗?

struct Station: Codable {

    let htmlAttributions: [String]
    let nextPageToken: String
    let status: String
    let result: Result

    struct Result: Codable {
        let id: String

        enum CodingKeys : String, CodingKey {
            case id
        }
    }

    enum CodingKeys : String, CodingKey {
        case htmlAttributions = "html_attributions"
        case nextPageToken = "next_page_token"
        case status
        case result = "resulsts"
    }
}

关于Swift4 的 JSON 解码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47534538/

相关文章:

javascript - 如何将此 JSON 文件中的图像正确附加到正确的 div 中

javascript - 从不同的键创建多层对象:value pairs

java - com.google.gson.JsonSyntaxException : java. lang.IllegalStateException:预期为 BEGIN_OBJECT 但在第 1 行第 1 列路径 $

json - 在 Docker 中将 JSON 文件作为环境变量传递

c - 在调试中得到不同的结果

c - 传递 'new_student' 的参数 1 使指针来自整数而不进行强制转换

c++ - 摆脱 FFmpeg 不存在的 PPS 错误

firefox - 从十六进制格式解码 Firefox 浏览器缓存条目

go - 解码Protobuf文字

使用 CUDA 复制结构内的指针数组