阅读 JSON "not an object"
Reading JSON "not an object"
我正在使用此 JSON library 从 Google 获取位置,地理编码方式如下:
let json = JSON(url: "https://maps.googleapis.com/maps/api/geocode/json?address=94127&key=MY_API_KEY")
print(json)
它打印这个:
{"results":[{"geometry":{"viewport":{"northeast":{"lat":37.7513029,"lng":-122.442329},"southwest":{"lat":37.721569,"lng":-122.472671}},"bounds":{"northeast":{"lat":37.7513029,"lng":-122.442329},"southwest":{"lat":37.721569,"lng":-122.472671}},"location":{"lat":37.734646,"lng":-122.463708},"location_type":"APPROXIMATE"},"formatted_address":"San Francisco, CA 94127, USA","types":["postal_code"],"address_components":[{"types":["postal_code"],"short_name":"94127","long_name":"94127"},{"types":["locality","political"],"short_name":"SF","long_name":"San Francisco"},{"types":["administrative_area_level_2","political"],"short_name":"San Francisco County","long_name":"San Francisco County"},{"types":["administrative_area_level_1","political"],"short_name":"CA","long_name":"California"},{"types":["country","political"],"short_name":"US","long_name":"United States"}],"place_id":"ChIJK9xbjZR9j4ARBsVPOdGWHs8"}],"status":"OK"}
格式化后,为了获取位置,我需要向下挖掘到 "results" -> "geometry" –> "location" –>"lat"/"lng"
。
尝试打印时:
print(json["results"]["geometry"]["location"]["lat"])
...我收到以下错误:
Error Domain=JSONErrorDomain Code=500 "not an object" UserInfo={NSLocalizedDescription=not an object}
我做错了什么?
谢谢!
问题是 lat/lng 不是 Json 对象,它们是 Json 数据。
尝试
print(json["results"]["geometry"]["location"].lat)
更新:
@Sulthan,感谢您对索引的评论。
print(json["results"][0]["geometry"]["location"].lat)
就像@Sulthan 提到的,"results"
是一个数组,我需要从中选择一个索引。
print(json["results"][0]["geometry"]...
@JianpingLiu 给我指明了正确的方向。要访问数据,我需要附加 .data
.
最终的解决方案是:
print(json["results"][0]["geometry"]["location"]["lat"].data)
我正在使用此 JSON library 从 Google 获取位置,地理编码方式如下:
let json = JSON(url: "https://maps.googleapis.com/maps/api/geocode/json?address=94127&key=MY_API_KEY")
print(json)
它打印这个:
{"results":[{"geometry":{"viewport":{"northeast":{"lat":37.7513029,"lng":-122.442329},"southwest":{"lat":37.721569,"lng":-122.472671}},"bounds":{"northeast":{"lat":37.7513029,"lng":-122.442329},"southwest":{"lat":37.721569,"lng":-122.472671}},"location":{"lat":37.734646,"lng":-122.463708},"location_type":"APPROXIMATE"},"formatted_address":"San Francisco, CA 94127, USA","types":["postal_code"],"address_components":[{"types":["postal_code"],"short_name":"94127","long_name":"94127"},{"types":["locality","political"],"short_name":"SF","long_name":"San Francisco"},{"types":["administrative_area_level_2","political"],"short_name":"San Francisco County","long_name":"San Francisco County"},{"types":["administrative_area_level_1","political"],"short_name":"CA","long_name":"California"},{"types":["country","political"],"short_name":"US","long_name":"United States"}],"place_id":"ChIJK9xbjZR9j4ARBsVPOdGWHs8"}],"status":"OK"}
格式化后,为了获取位置,我需要向下挖掘到 "results" -> "geometry" –> "location" –>"lat"/"lng"
。
尝试打印时:
print(json["results"]["geometry"]["location"]["lat"])
...我收到以下错误:
Error Domain=JSONErrorDomain Code=500 "not an object" UserInfo={NSLocalizedDescription=not an object}
我做错了什么?
谢谢!
问题是 lat/lng 不是 Json 对象,它们是 Json 数据。
尝试
print(json["results"]["geometry"]["location"].lat)
更新:
@Sulthan,感谢您对索引的评论。
print(json["results"][0]["geometry"]["location"].lat)
就像@Sulthan 提到的,"results"
是一个数组,我需要从中选择一个索引。
print(json["results"][0]["geometry"]...
@JianpingLiu 给我指明了正确的方向。要访问数据,我需要附加 .data
.
最终的解决方案是:
print(json["results"][0]["geometry"]["location"]["lat"].data)