使用 simplejson 读取 JSON python
readingJSON with simplejson python
我在使用 urllib2 发送 url 请求后尝试读取 JSON。
我的代码:
request = urllib2.Request("https://127.0.0.1:443/myAPI", data=form_data, headers=headers)
response = urllib2.open(request)
所以问题是当我试图从响应对象读取 JSON 时。
我就是这样做的
simplejson.loads(response.read())
我得到的错误是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/new/main3/python/simplejson/__init__.py", line 385, in loads
return _default_decoder.decode(s)
File "/opt/new/main3/python/simplejson/decoder.py", line 402, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/opt/new/main3/python/simplejson/decoder.py", line 420, in raw_decode
raise JSONDecodeError("No JSON object could be decoded", s, idx)
simplejson.decoder.JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)
有趣 部分是当我在我的 firefox 浏览器中发送请求时,我输入地址 127.0.0.1/myAPI 在 url 行,我确实在屏幕上看到 json 并且我可以在调试模式下看到它作为 JSON
{"hosts": [{"host": "127.0.0.1:4448"}]}
所以 json 有效..
在调试时我得到这个页面:
<!DOCTYPE html>
<html>
<head>
<!-- framebusting -->
<style>
html{display : none ;}
</style>
<script type="text/javascript">
if (self == top) {
document.documentElement.style.display = "block";
} else {
top.location = self.location;
}
</script>
<script type="text/javascript" src="js/detectBrowser.js"></script>
<meta charset="utf-8"/>
<link rel="StyleSheet" href="css/forensics.css" type="text/css" media="screen"/>
</head>
<body>
<script type="text/javascript" src="libs/requirejs/require.js" data-main="js/login.js"></script>
</body>
</html>
有没有人有办法解决它,或者我如何直接从响应对象读取 json 文本,或者甚至在调试
中查看它
感谢蚂蚁的帮助,我正在尝试至少 3 天的时间来弄清楚
谢谢
我想是因为这个simplejson.loads(response .read())
。您在响应和 .read()
之间有一个 space
尝试以下操作:
request = urllib2.Request("https://127.0.0.1:443/myAPI", data=form_data, headers=headers)
response = urllib2.open(request)
response_body = response.read()
response_body_json = simplejson.loads(response_body)
所以这是我的错,api 需要一个 cookie。
添加 cookie 后 header 我收到了正确的 JSON
我在使用 urllib2 发送 url 请求后尝试读取 JSON。
我的代码:
request = urllib2.Request("https://127.0.0.1:443/myAPI", data=form_data, headers=headers)
response = urllib2.open(request)
所以问题是当我试图从响应对象读取 JSON 时。 我就是这样做的
simplejson.loads(response.read())
我得到的错误是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/new/main3/python/simplejson/__init__.py", line 385, in loads
return _default_decoder.decode(s)
File "/opt/new/main3/python/simplejson/decoder.py", line 402, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/opt/new/main3/python/simplejson/decoder.py", line 420, in raw_decode
raise JSONDecodeError("No JSON object could be decoded", s, idx)
simplejson.decoder.JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)
有趣 部分是当我在我的 firefox 浏览器中发送请求时,我输入地址 127.0.0.1/myAPI 在 url 行,我确实在屏幕上看到 json 并且我可以在调试模式下看到它作为 JSON {"hosts": [{"host": "127.0.0.1:4448"}]}
所以 json 有效..
在调试时我得到这个页面:
<!DOCTYPE html>
<html>
<head>
<!-- framebusting -->
<style>
html{display : none ;}
</style>
<script type="text/javascript">
if (self == top) {
document.documentElement.style.display = "block";
} else {
top.location = self.location;
}
</script>
<script type="text/javascript" src="js/detectBrowser.js"></script>
<meta charset="utf-8"/>
<link rel="StyleSheet" href="css/forensics.css" type="text/css" media="screen"/>
</head>
<body>
<script type="text/javascript" src="libs/requirejs/require.js" data-main="js/login.js"></script>
</body>
</html>
有没有人有办法解决它,或者我如何直接从响应对象读取 json 文本,或者甚至在调试
中查看它感谢蚂蚁的帮助,我正在尝试至少 3 天的时间来弄清楚 谢谢
我想是因为这个simplejson.loads(response .read())
。您在响应和 .read()
尝试以下操作:
request = urllib2.Request("https://127.0.0.1:443/myAPI", data=form_data, headers=headers)
response = urllib2.open(request)
response_body = response.read()
response_body_json = simplejson.loads(response_body)
所以这是我的错,api 需要一个 cookie。 添加 cookie 后 header 我收到了正确的 JSON