Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The key to understanding <code>jsonResponse</code>'s format is to print it out:</p> <pre><code>import urllib, json import pprint URL2 = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&amp;sensor=false" googleResponse = urllib.urlopen(URL2) jsonResponse = json.loads(googleResponse.read()) pprint.pprint(jsonResponse) # {u'results': [{u'address_components': [{u'long_name': u'1600', # u'short_name': u'1600', # u'types': [u'street_number']}, # {u'long_name': u'Amphitheatre Pkwy', # u'short_name': u'Amphitheatre Pkwy', # u'types': [u'route']}, # {u'long_name': u'Mountain View', # u'short_name': u'Mountain View', # u'types': [u'locality', # u'political']}, # {u'long_name': u'San Jose', # u'short_name': u'San Jose', # u'types': [u'administrative_area_level_3', # u'political']}, # {u'long_name': u'Santa Clara', # u'short_name': u'Santa Clara', # u'types': [u'administrative_area_level_2', # u'political']}, # {u'long_name': u'California', # u'short_name': u'CA', # u'types': [u'administrative_area_level_1', # u'political']}, # {u'long_name': u'United States', # u'short_name': u'US', # u'types': [u'country', # u'political']}, # {u'long_name': u'94043', # u'short_name': u'94043', # u'types': [u'postal_code']}], # u'formatted_address': u'1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA', # u'geometry': {u'location': {u'lat': 37.4216227, # u'lng': -122.0840263}, # u'location_type': u'ROOFTOP', # u'viewport': {u'northeast': {u'lat': 37.424770299999999, # u'lng': -122.0808787}, # u'southwest': {u'lat': 37.418475100000002, # u'lng': -122.0871739}}}, # u'types': [u'street_address']}], # u'status': u'OK'} test = json.dumps([s['geometry']['location'] for s in jsonResponse['results']], indent=3) print(test) # [ # { # "lat": 37.4216227, # "lng": -122.0840263 # } # ] </code></pre> <ol> <li><code>jsonResponse</code> is a dict.</li> <li><code>jsonResponse['results']</code> is a list of dicts.</li> <li>The loop <code>for s in jsonResponse['results']</code> assigns <code>s</code> to a dict for each iteration through the loop.</li> <li><code>s['geometry']</code> is a dict.</li> <li><code>s['geometry']['location']</code> (finally!) contains the latitude/longitude dict.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload