【问题标题】:How To Decode Json Data From Google Geocoding API Swift如何从 Google Geocoding API Swift 解码 Json 数据
【发布时间】:2020-12-05 23:51:04
【问题描述】:

我正在向 google 地理编码 api 发送地址字符串以获得该地址的经度和纬度,但是当我获取 JSON 数据时,我正在努力解码返回的 JSON 数据。我认为应该将数据放入一个结构中,但是其中的数据具有不同的数组,我不确定如何构造它。

获取地理编码位置的代码:

func longLatLookUp() {
        
         let url = URL(string: "https://maps.googleapis.com/maps/api/geocode/json?address=10+Princes+Street+Edinburgh+Scotland&key={ API key }")!

        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            guard let data = data else { return }
            print(String(data: data, encoding: .utf8)!)
        }
        task.resume()
        
    }

控制台返回:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "10",
               "short_name" : "10",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Princes Street",
               "short_name" : "Princes St",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Edinburgh",
               "short_name" : "Edinburgh",
               "types" : [ "postal_town" ]
            },
            {
               "long_name" : "Edinburgh",
               "short_name" : "Edinburgh",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Scotland",
               "short_name" : "Scotland",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United Kingdom",
               "short_name" : "GB",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "EH2 2AN",
               "short_name" : "EH2 2AN",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "10 Princes St, Edinburgh EH2 2AN, UK",
         "geometry" : {
            "location" : {
               "lat" : 55.95357130000001,
               "lng" : -3.1900314
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 55.95492028029151,
                  "lng" : -3.188682419708498
               },
               "southwest" : {
                  "lat" : 55.95222231970851,
                  "lng" : -3.191380380291502
               }
            }
         },
         "place_id" : "ChIJweMPNY7Hh0gRWMp_nlFlOO0",
         "plus_code" : {
            "compound_code" : "XR35+CX Edinburgh, UK",
            "global_code" : "9C7RXR35+CX"
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

【问题讨论】:

  • 将您的 JOSN 粘贴到 quicktyp.io 中,它将帮助您学习如何构建结构。
  • 谢谢!这是一个很大的帮助,帮助我更好地理解结构和解码 JSON 数据。

标签: swift xcode google-maps geocoding google-geocoding-api


【解决方案1】:

编辑:

plus_code 可以为 nil,所以我把它改成了可选的。

struct Response: Decodable {
    let results: [Component]
    let status: String

    struct Component: Decodable {
        let address_components: [Address]
        let formatted_address: String
        let geometry: Geometry
        let place_id: String
        let plus_code: PlusCode?
        let types: [String]
    }

    struct Address: Decodable {
        let long_name: String
        let short_name: String
        let types : [String]
    }

    struct Geometry: Decodable {
        let location: Location
        let location_type: String
        let viewport: Viewport
    }

    struct Viewport: Decodable {
        let northeast: Location
        let southwest: Location
    }

    struct PlusCode: Decodable {
        let compound_code: String
        let global_code: String
    }

    struct Location: Decodable {
        let lat: Float
        let lng: Float
    }
}

你可以像这样解码数据:

let json = try! JSONDecoder().decode(Response.self, from: data)

【讨论】:

  • 感谢这个结构的结构真的帮助了我!但是,我需要为每个枚举添加一个 CodingKeys 枚举才能使其正常工作。
  • 很高兴听到它有帮助!但它不应该需要 CodingKeys,因为所有属性名称都与 JSON 键匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多