json - Parsing data from MapQuest reverse geocoding api in Python? -
my code:
from urllib import request import json lat = 31.33 ; long = -84.52 webpage = "http://www.mapquestapi.com/geocoding/v1/reverse?key=my_key&callback=renderreverse&location={},{}".format(lat, long) response = request.urlopen(webpage) json_data = response.read().decode(response.info().get_param('charset') or 'utf-8') data = json.loads(json_data) print(data)
this gives me following error:
valueerror: expecting value: line 1 column 1 (char 0)
i trying read county , state mapquest reverse geocoding api. response looks this:
renderreverse({"info":{"statuscode":0,"copyright":{"text":"\u00a9 2015 mapquest, inc.","imageurl":"http://api.mqcdn.com/res/mqlogo.gif","imagealttext":"\u00a9 2015 mapquest, inc."},"messages":[]},"options":{"maxresults":1,"thumbmaps":true,"ignorelatlnginput":false},"results":[{"providedlocation":{"latlng":{"lat":32.841516,"lng":-83.660992}},"locations":[{"street":"562 patterson st","adminarea6":"","adminarea6type":"neighborhood","adminarea5":"macon","adminarea5type":"city","adminarea4":"bibb","adminarea4type":"county","adminarea3":"ga","adminarea3type":"state","adminarea1":"us","adminarea1type":"country","postalcode":"31204-3508","geocodequalitycode":"p1aaa","geocodequality":"point","dragpoint":false,"sideofstreet":"l","linkid":"0","unknowninput":"","type":"s","latlng":{"lat":32.84117,"lng":-83.660973},"displaylatlng":{"lat":32.84117,"lng":-83.660973},"mapurl":"http://www.mapquestapi.com/staticmap/v4/getmap?key=my_key&type=map&size=225,160&pois=purple-1,32.84117,-83.660973,0,0,|¢er=32.84117,-83.660973&zoom=15&rand=-189494136"}]}]})
how convert string dict can query using key? appreciated. thanks!
first, rid of callback parameter in url since that's causing response wrapped in renderreverse()
webpage = "http://www.mapquestapi.com/geocoding/v1/reverse?key=my_key&location={},{}".format(lat, long)
that give valid json should work json.loads function call. @ point can interact data dictionary, getting county , state names keys. way mapquests structures json pretty strange, looks may have string matching correct key name. in case, 'adminarea4type' set 'county' want access 'adminarea4' key return county name.
data['results'][0]['locations'][0]['adminarea4']
Comments
Post a Comment