【问题标题】:Python error load JSON code of google APIPython错误加载谷歌API的JSON代码
【发布时间】:2016-04-07 00:08:33
【问题描述】:

我正在使用 google geocode API 测试以下 Python3.5 代码,但收到以下错误。

raise JSONDecodeError("Expecting value", s, err.value) from None >JSONDecodeError: Expecting value

代码如下:

import urllib
import json

serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'

while True:
    address = input('Enter location: ')
    if len(address) < 1 : break

    url = serviceurl + urllib.parse.urlencode({'sensor':'false',
       'address': address})
    print ('Retrieving', url)
    uh = urllib.request.urlopen(url)
    data = uh.read()
    print ('Retrieved',len(data),'characters')

    js = json.loads(str(data))

知道我为什么会出错。

【问题讨论】:

  • 当您将具有None、空白字符串或其他不兼容数据类型的变量传递到json.loads(...) 时会产生此错误,请参阅:docs.python.org/3/library/json.html 如果您直接从文件加载 json,您应该使用myjson = json.load(open("yourjson.json", 'r')) 在内存中生成python dict 数据类型。

标签: python json google-geocoding-api


【解决方案1】:

出现错误是因为“数据”是字节类型,因此您必须先将其解码为字符串,然后才能使用json.loads 将其转换为 json 对象。所以解决问题:

uh = urllib.request.urlopen(url)
data = uh.read()
print ('Retrieved',len(data),'characters')

js = json.loads(data.decode("utf-8"))

此外,您共享的代码中的 str(data) 将在 Python 2.x 中有效,但在 Python 3.x 中无效,因为 str() 在 3.x 中不会将字节转换为字符串。

【讨论】:

    【解决方案2】:

    查看错误:

    "raise JSONDecodeError("Expecting value", s, err.value) from None

    JSONDecodeError: 期望值"

    意思是当我应该得到一些东西时,我得到了一个无。

    在调用json.loads() 之前检查您的数据变量是否为无。

    if data == None or data == '':
      print('I got a null or empty string value for data in a file')
    else:
      js = json.loads(str(data))
    

    【讨论】:

      【解决方案3】:

      所以,我必须修改您的代码才能运行。我在 Ubuntu 14.04 上使用 Python 3.4.3。

      #import urllib  
      import urllib.parse
      import urllib.request
      

      我收到了类似的错误:

      heyandy889@laptop:~/src/test$ python3 help.py 
      Enter location: MI
      Retrieving http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=MI
      Retrieved 1405 characters
      Traceback (most recent call last):
        File "help.py", line 18, in <module>
          js = json.loads(str(data))
        File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
          return _default_decoder.decode(s)
        File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
          obj, end = self.raw_decode(s, idx=_w(s, 0).end())
        File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
          raise ValueError(errmsg("Expecting value", s, err.value)) from None
      ValueError: Expecting value: line 1 column 1 (char 0)
      

      基本上,我们不是尝试解码有效的 json 字符串,而是尝试解码 Python 的“None”值,它不是有效的 json。尝试在以下示例代码中进行修补。首先运行一次以仔细检查最简单的 json 对象“{}”是否可以工作。然后,逐个尝试每个不同的 'possible_json_string'。

      #...
      print ('Retrieved',len(data),'characters')
      
      #possible_json_string = str(data) #original error
      possible_json_string = '{}' #sanity check with simplest json
      #possible_json_string = data #why convert to string at all?
      #possible_json_string = data.decode('utf-8') #intentional conversion
      
      print('possible_json_string')
      print(possible_json_string)
      js = json.loads(possible_json_string)
      

      Source

      【讨论】:

        猜你喜欢
        • 2017-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-27
        • 1970-01-01
        相关资源
        最近更新 更多