【问题标题】:No JSON object could be decoded after retrieving JSON content using POST without Gzip encoding使用没有 Gzip 编码的 POST 检索 JSON 内容后,无法解码任何 JSON 对象
【发布时间】:2015-07-10 07:50:41
【问题描述】:

使用 Python 2.7.8,如果运行此脚本,我们会得到“ValueError: No JSON object could be decoded”:

from urllib2 import urlopen, Request
from json import dumps, loads, load

values = dumps({
    "issueId": 10600,
    "versionId": "10000",
    "cycleId": "16",
    "projectId": 10000
})
headers = {"Content-Type": "application/json"}
request = Request("http://private-anon-491d363a1-getzephyr.apiary-mock.com/jira_server/rest/zapi/latest/execution", data=values, headers=headers)
print request.get_method()
print request.get_header("Content-Encoding")

response = urlopen(request)
print "\n" + response.read()
print response.getcode()
print response.geturl()
print response.info()
json = loads(response.read())  # raises ValueError("No JSON object could be decoded")

输出是:

POST
None

{
    "32": {
        "id": 32,
        "executionStatus": "-1",
        "comment": "",
        "htmlComment": "",
        "cycleId": 16,
        "cycleName": "Audit Test Cycle 3",
        "versionId": 10000,
        "versionName": "v1",
        "projectId": 10000,
        "issueId": 10600,
        "issueKey": "ZFJ-19",
        "summary": "test - check1",
        "label": "",
        "component": ""
    }
}
200
http://private-anon-491d363a1-getzephyr.apiary-mock.com/jira_server/rest/zapi/latest/execution
Server: Cowboy
X-Apiary-Ratelimit-Limit: 120
X-Apiary-Ratelimit-Remaining: 119
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT
Access-Control-Max-Age: 10
X-Apiary-Transaction-Id: 55423c1c7996e10300e32acc
Date: Thu, 30 Apr 2015 14:28:45 GMT
X-Cache: MISS from p-proxy.int.hrs.com
X-Cache-Lookup: MISS from p-proxy.int.hrs.com:3128
Via: 1.1 vegur, 1.0 p-proxy.int.hrs.com:3128 (squid/2.6.STABLE6)
Proxy-Connection: close

Traceback (most recent call last):
  File "test2.py", line 20, in <module>
    json = loads(response.read())  # raises ValueError("No JSON object could be decoded")
  File "C:\Program Files (x86)\python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files (x86)\python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files (x86)\python27\lib\json\decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

我阅读了很多涉及 Gzip 和 requests lib 等方面的问答,但没有帮助。如何使用 urlib2json 库解码响应?如果我将响应复制到一个文件中并解析该文件,它就可以工作,因此它是有效的 json。

【问题讨论】:

    标签: python json python-2.7 urllib2 jira-zephyr


    【解决方案1】:

    你不能 .read() 文件类对象(根据 docs urlopen 返回)两次。我修改了你的代码,试试看:

    from urllib2 import urlopen, Request
    from json import dumps, loads, load
    
    values = dumps({
        "issueId": 10600,
        "versionId": "10000",
        "cycleId": "16",
        "projectId": 10000
    })
    headers = {"Content-Type": "application/json"}
    request = Request("http://private-anon-491d363a1-getzephyr.apiary-mock.com/jira_server/rest/zapi/latest/execution", data=values, headers=headers)
    print request.get_method()
    print request.get_header("Content-Encoding")
    
    response = urlopen(request)
    text = response.read()         #storing the data
    print "\n" + text              #note the usage of the stored string
    print response.getcode()
    print response.geturl()
    print response.info()
    json = loads(text)             #note the usage of the stored string
    

    【讨论】:

    • post方法怎么样?
    【解决方案2】:

    您应该将内容存储到一个变量中并对该变量进行解码。 response.read() 的 sencod 调用不会得到任何数据。

    简单的解决方案:

    content = response.read()
    json = loads(content)
    

    【讨论】:

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