Python 在使用POST检索JSON内容而不使用Gzip编码后,无法解码任何JSON对象

Python 在使用POST检索JSON内容而不使用Gzip编码后,无法解码任何JSON对象,python,json,python-2.7,urllib2,jira-zephyr,Python,Json,Python 2.7,Urllib2,Jira Zephyr,使用Python 2.7.8,如果运行以下脚本,我们会得到“ValueError:无法解码任何JSON对象”: from urllib2 import urlopen, Request from json import dumps, loads, load values = dumps({ "issueId": 10600, "versionId": "10000", "cycleId": "16", "projectId": 10000 }) headers

使用Python 2.7.8,如果运行以下脚本,我们会得到“ValueError:无法解码任何JSON对象”:

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
POST
没有一个
{
"32": {
“id”:32,
“执行状态”:“-1”,
“评论”:“评论”,
“htmlComment”:“,
“cycleId”:16,
“cycleName”:“审核测试周期3”,
“versionId”:10000,
“版本名称”:“v1”,
“Projectd”:10000,
“发行编号”:10600,
“issueKey”:“ZFJ-19”,
“总结”:“测试-检查1”,
“标签”:“,
“组件”:”
}
}
200
http://private-anon-491d363a1-getzephyr.apiary-mock.com/jira_server/rest/zapi/latest/execution
服务员:牛仔
X-养蜂场限额-限额:120
X-养蜂场-剩余差饷限额:119
内容类型:application/json
访问控制允许来源:*
访问控制允许方法:选项、获取、HEAD、POST、PUT、删除、跟踪、连接
访问控制最大年龄:10岁
X-Apiary-Transaction-Id:55423c1c7996e10300e32acc
日期:2015年4月30日星期四14:28:45 GMT
X-Cache:p-proxy.int.hrs.com未命中
X-Cache-Lookup:p-proxy.int.hrs.com中的未命中:3128
Via:1.1vegur,1.0p-proxy.int.hrs.com:3128(squid/2.6.STABLE6)
代理连接:关闭
回溯(最近一次呼叫最后一次):
文件“test2.py”,第20行,在
json=loads(response.read())#引发ValueError(“无法解码json对象”)
文件“C:\Program Files(x86)\python27\lib\json\\uuuu init\uuuu.py”,第338行,加载
返回\u默认\u解码器。解码
文件“C:\Program Files(x86)\python27\lib\json\decoder.py”,第366行,解码中
obj,end=self.raw\u decode(s,idx=\u w(s,0.end())
文件“C:\ProgramFiles(x86)\python27\lib\json\decoder.py”,第384行,原始解码
raise VALUERROR(“无法解码JSON对象”)
ValueError:无法解码任何JSON对象
我读了很多涉及Gzip和
请求
lib等方面的问答,但都没有帮助。如何使用
urlib2
json
libs来解码响应??如果我将响应复制到一个文件中并解析该文件,它将工作,因此它是有效的json。

您不能将
.read()
类似文件的对象(根据
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

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

简单解决方案:

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

post方法怎么样?