【问题标题】:No JSON object could be decoded无法解码任何 JSON 对象
【发布时间】:2013-01-19 01:15:12
【问题描述】:

运行此脚本时出现以下错误...

ERROR: getTest failed.  No JSON object could be decoded

我的代码是:

import json
import urllib

class test:

    def getTest(self):
        try:
            url = 'http://www.exapmle.com/id/123456/'
            json_ = urllib.urlopen(url).read()
            self.id = json.loads(json_)
            print self.id
        except Exception, e:
            print "ERROR: getTest failed.  %s" % e
if __name__ == "__main__":
    ti = test()
    ti.getTest()

在浏览器中打开此网址“http://www.exapmle.com/id/123456/”时,我正在获取 json 格式的数据,但为什么会抛出错误?

【问题讨论】:

  • 您能否发布从 URL 中返回的内容以便我们查看?在这种情况下,我只能假设问题出在格式错误的 JSON 上,如果没有相关的 JSON,就很难调试。

标签: json python-2.6


【解决方案1】:

在调用json.loads() 之前打印json_。如果您的url 中有错误,urllib 不一定会抛出异常,它可能只是检索一些错误页面(如提供的 url),这是无效的 json。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多