解析 JSON in Swift 3 (CLLocationCoordinate2D)

Parsing JSON in Swift 3 (CLLocationCoordinate2D)

我正在尝试用 Google 地点 iOS API 解析 JSON link。当我打印坐标时,它显示为

CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)

而不是地点的实际坐标。我认为这可能是一个 dictionary/object 问题,但我找不到解决方案。

下面是我的JSON解析代码:

func parseJsonData(data: NSData) -> [GymGooglePlace] {

    do {

        typealias JSONDictionary = [String:Any]

        if let parsedData = try JSONSerialization.jsonObject(with: data as Data) as? JSONDictionary, let gyms = parsedData["results"] as? [JSONDictionary] {

            for gym in gyms {

                var coordinate = CLLocationCoordinate2D()
                var latitude:CLLocationDegrees!
                var longitude:CLLocationDegrees!

                var maxWidth:Int!
                var photoReference:String!
                let photoURL:String!

                if let geometry = gym["geometry"] as? [JSONDictionary] {

                    for result in geometry {

                        if let locations = result["location"] as? [JSONDictionary] {

                            for location in locations {

                                latitude = location["lat"] as! CLLocationDegrees!
                                longitude = location["lng"] as! CLLocationDegrees!

                                coordinate.latitude = latitude
                                coordinate.longitude = longitude
                            }
                        }
                    }
                }

                if let photos = gym["photos"] as? [JSONDictionary] {

                    for photo in photos {

                        maxWidth = photo["width"] as! Int!
                        photoReference = photo["photo_reference"] as! String!
                    }
                }

                photoURL = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=\(maxWidth!)&photoreference=\(photoReference!)&key=\(googleAPIKey)"

                print(photoURL)
                print(coordinate)
                print("spacespacespacespacespacespacespacespacespace")
            }
        }



    } catch {
        print(error)
    }

    return gymGooglePlaces
}

geometrylocation的值是字典,请注意{},数组是[]

 if let geometry = gym["geometry"] as? JSONDictionary,
    let location = geometry["location"] as? JSONDictionary {
         coordinate.latitude = location["lat"] as! CLLocationDegrees
         coordinate.longitude = location["lng"] as! CLLocationDegrees
 }