简体   繁体   中英

JSON return to python iterable

I am trying to get this return:

Google API

Into a Python iterable object

So far I have this:

request = "http://maps.googleapis.com/maps/api/geocode/%s?address=%s&sensor=%s" % (self._output, self._address, self._sensor)
data = urllib.urlopen(request).read()
decoded_data = json.loads('[%s]' % data).pop()
    if decoded_data.get("status") == "OK":
        return decoded_data
    return ""

However this only turns the outer "wrapper" into a dictionary. I want to decode the entire block so I can access the values easily and raise exceptions where needed.

Why are you wrapping it in a list? It's a JSON object, which maps very cleanly to a Python dictionary. Just do decoded_data = json.loads(data) , then you can do decoded_data['results'][0]['formatted_address'] etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM