简体   繁体   中英

In python, ValueError: No JSON object could be decoded

I'm trying to connect my code with google maps api(geocoding API) key. But when I run my code, the ValueError occurs. I don't know why. It might be silly, but please help me.

Here's my python code.

import httplib2 
import json 

def getGeocodeLocation(inputString): 
    google_api_key = "MY_API_KEY" 
    locationString = inputString.replace(" ", "+") 
    url = ('https://maps.googleapis.com/maps/api/geocode/json?address=%s & key=%s' % (locationString, google_api_key)) 
    h = httplib2.Http() 
    response, content = h.request(url, 'GET') 
    result = json.loads(content) 
    print "response header: %s \n \n" % response 
    return result

And here's the error.

>>> from geocode import getGeocodeLocation
>>> getGeocodeLocation("Dalls, Texas")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "geocode.py", line 10, in getGeocodeLocation
    result = json.loads(content)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Thanks.

+

When I edited the code from,

    response, content = h.request(url, 'GET') 
    result = json.loads(content) 
    print "response header: %s \n \n" % response 

to

    response, content = h.request(url, 'GET')  
    print "response header: %s \n \n" % response 
    result = json.loads(content)

The result was like this.

{u'status': u'OK', 
u'results': 
    [
    {u'geometry': 
        {u'location_type': u'APPROXIMATE', 
        u'bounds': 
            {u'northeast': {u'lat': 33.0237921, u'lng': -96.4637379}, 
            u'southwest': {u'lat': 32.617537, u'lng': -96.999347}}, 
        u'viewport': 
            {u'northeast': {u'lat': 33.0237921, u'lng': -96.4637379}, 
            u'southwest': {u'lat': 32.617537, u'lng': -96.999347}}, 
        u'location': {u'lat': 32.7766642, u'lng': -96.79698789999999}
        }, 
    u'address_components': 
        [
        {u'long_name': u'Dallas', 
        u'types': [u'locality', u'political'], 
        u'short_name': u'Dallas'}, 
        {u'long_name': u'Dallas County', 
        u'types': [u'administrative_area_level_2', u'political'], 
        u'short_name': u'Dallas County'}, 
        {u'long_name': u'Texas', 
        u'types': [u'administrative_area_level_1', u'political'], 
        u'short_name': u'TX'}, 
        {u'long_name': u'United States', 
        u'types': [u'country', u'political'], 
        u'short_name': u'US'}
        ], 
    u'place_id': u'ChIJS5dFe_cZTIYRj2dH9qSb7Lk', 
    u'formatted_address': u'Dallas, TX, USA', 
    u'types': [u'locality', u'political']
    }
    ]
}

You are having an error in your request. The status code is 400 Bad Request, and the content of your response is:

Your client has issued a malformed or illegal request. That's all we know.

There error seems to be on this line:

url = ('https://maps.googleapis.com/maps/api/geocode/json?address=%s & key=%s' % (locationString, google_api_key))

You shouldn't have blank spaces around the & sign:

url = ('https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s' % (locationString, google_api_key))

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