我的解码器没有在Swift 4中看到我的一个JSON对象

时间:2018-03-31 21:04:14

标签: json swift4 codable

我正在尝试使用Codable协议来解码FAA网站上的JSON。它似乎与JSON的一些子部分很好地工作,但无论我尝试什么,我都无法弄清楚我做错了什么。我将正在读取的部分设置为与被忽略的部分完全相同。

这是我的Codable模型:

struct Runway: Codable {
    var type: String
    var features: [Features]
    enum codingKeys: String, CodingKey {
        case type = "type"
        case features = "features"
    }
    struct Features: Codable {
        var type: String
        var properties: Properties
        var geometry: Geometry
        enum codingKeys: String, CodingKey {
            case type = "type"
            case properties = "properties"
            case geometry = "geometry"
        }
        struct Properties: Codable {
            var objectID: Int?
            var globalID: String?
            var airportID: String?
            var designator: String?
            var length: Int?
            var width: Int?
            var lightActivity: Int?
            var lightInt: String?

            private enum codingKeys: String, CodingKey {
                case objectID = "OBJECTID"
                case globalID = "GLOBAL_ID"
                case airportID = "AIRPORT_ID"
                case designator = "DESIGNATOR"
                case length = "LENGTH"
                case width = "WIDTH"
                case lightActivity = "LIGHTACTV"
                case lightInt = "LIGHTINTNS"
            }
        }
        struct Geometry: Codable {
            var type: String
            var coordinates: [[[Double]]]
            enum codingKeys: String, CodingKey {
                case type = "type"
                case coordinates = "coordinates"
            }
        }
    }
}

以下是第一对JSON元素:(抱歉格式化,无论我尝试什么,我都无法让JSON看起来正确。但是我得到的只是属性结构。属性结构返回零。

{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "properties":{
            "OBJECTID":3001,
            "GLOBAL_ID":"B5559C52-205D-479D-800A-C462015FCCE8",
            "AIRPORT_ID":"98D2CC5B-EBC9-4901-8B72-AB5596FFDD02",
            "DESIGNATOR":"13",
            "LENGTH":3499,
            "WIDTH":77,
            "DIM_UOM":"FT",
            "COMP_CODE":"ASPH",
            "LIGHTACTV":2,
            "LIGHTINTNS":"LIM",
            "AK_LOW":0,
            "AK_HIGH":0,
            "US_LOW":1,
            "US_HIGH":0,
            "US_AREA":0,
            "PACIFIC":0,
            "Shape__Area":0.00000278998996340473,
            "Shape__Length":0.0235944263922133
         },
         "geometry":{
            "type":"Polygon",
            "coordinates":[
               [
                  [
                     -91.4991881305143,
                     43.5930572596937,
                     0
                  ],
                  [
                     -91.4993937775553,
                     43.5929079896572,
                     0
                  ],
                  [
                     -91.5087273920645,
                     43.599699649116,
                     0
                  ],
                  [
                     -91.5085217450235,
                     43.5998489351524,
                     0
                  ],
                  [
                     -91.4991881305143,
                     43.5930572596937,
                     0
                  ]
               ]
            ]
         }
      },
      {
         "type":"Feature",
         "properties":{
            "OBJECTID":3002,
            "GLOBAL_ID":"82E74ABD-6CCA-4505-8B1A-468A5A1B4058",
            "AIRPORT_ID":"18DFA099-66A1-4AE5-B2F8-C62052139996",
            "DESIGNATOR":"16",
            "LENGTH":4001,
            "WIDTH":75,
            "DIM_UOM":"FT",
            "COMP_CODE":"ASPH",
            "LIGHTACTV":2,
            "LIGHTINTNS":"LIM",
            "AK_LOW":0,
            "AK_HIGH":0,
            "US_LOW":1,
            "US_HIGH":0,
            "US_AREA":0,
            "PACIFIC":0,
            "Shape__Area":0.00000321253134576419,
            "Shape__Length":0.0235103894432249
         },
         "geometry":{
            "type":"Polygon",
            "coordinates":[
               [
                  [
                     -93.2617071279774,
                     45.5523006585432,
                     0
                  ],
                  [
                     -93.2619863670487,
                     45.5522388825147,
                     0
                  ],
                  [
                     -93.2666779488979,
                     45.5627046006791,
                     0
                  ],
                  [
                     -93.2663986638266,
                     45.5627663877077,
                     0
                  ],
                  [
                     -93.2617071279774,
                     45.5523006585432,
                     0
                  ]
               ]
            ]
         }
      }
   ]
}

1 个答案:

答案 0 :(得分:0)

您只需将codingKeys替换为CodingKeys即可。