0

I am using this JSON library to get a location from Google Geocoding the following way:

let json = JSON(url: "https://maps.googleapis.com/maps/api/geocode/json?address=94127&key=MY_API_KEY")
print(json)

It prints this:

{"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"}

Once formatted, to get the location, I need to dig down to "results" -> "geometry" –> "location" –>"lat"/"lng".
When trying to print this:

print(json["results"]["geometry"]["location"]["lat"])

...I am getting the following error:

Error Domain=JSONErrorDomain Code=500 "not an object" UserInfo={NSLocalizedDescription=not an object}

What am I doing wrong?
Thanks!

4
  • 1
    results is an array, so there should be some indexing before geometry. Commented Oct 15, 2016 at 14:55
  • Like how? print(json["results"][0]["geometry"]…? Commented Oct 15, 2016 at 14:56
  • I don't know that library but something like that. Commented Oct 15, 2016 at 14:58
  • Thanks! Your suggestion and @JianpingLiu's answer fixed it. Commented Oct 15, 2016 at 14:59

2 Answers 2

1

Like @Sulthan mentioned, "results" is an array of which I need to pick an index.

print(json["results"][0]["geometry"]...

@JianpingLiu hinted me to the right direction. To access the data, I need to append .data.

The final solution therefor is:

print(json["results"][0]["geometry"]["location"]["lat"].data)
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is lat/lng is not Json object, they are Json data.

Try

print(json["results"]["geometry"]["location"].lat)

Updated:

@Sulthan, Thanks for the comment about the index thing.

  print(json["results"][0]["geometry"]["location"].lat)

2 Comments

But Xcode doesn't know there is a .lat. I changed it (as suggested by Xcode) to .data but that gives me nil
Thank you too! You directed me to the solution. See my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.