在Swift 3中解析JSON(CLLocationCoordinate2D)

在Swift 3中解析JSON(CLLocationCoordinate2D),json,swift,google-places-api,Json,Swift,Google Places Api,我试图用GooglePlacesIOSAPI解析JSON链接。当我打印坐标时,它显示为 CLLocationCoordinate2D(纬度:0.0,经度:0.0) 而不是这个地方的实际坐标。我认为这可能是一个字典/对象问题,但我找不到解决方案 下面是我的JSON解析代码: func parseJsonData(data: NSData) -> [GymGooglePlace] { do { typealias JSONDictionary = [String:A

我试图用GooglePlacesIOSAPI解析JSON链接。当我打印坐标时,它显示为

CLLocationCoordinate2D(纬度:0.0,经度:0.0)

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

下面是我的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
}

几何体
位置
的值是字典,请注意
{}
,数组是
[]

 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
 }

您的JSON看起来像什么?您是否确保获得了实际的lat和long值,即结果[“location”]实际上包含一些数据?老实说,我过去常常手动解析JSON负载,因此不需要拉入任何第三方库。但现在,我喜欢ObjectMapper或SwiftyJson之类的播客。因此,当JSON有效负载结构发生变化时,您需要做的只是改变键路径,而不是重写解析方法