[英]readingJSON with simplejson python
i'm trying to read JSON after i'm sending url request with urllib2. 我使用urllib2发送url请求后尝试读取JSON。
my code: 我的代码:
request = urllib2.Request("https://127.0.0.1:443/myAPI", data=form_data, headers=headers)
response = urllib2.open(request)
So the problem is when i'm trying to read to JSON from the respond object. 所以问题是当我尝试从响应对象读取JSON时。 i'm doing it like that 我正在那样做
simplejson.loads(response.read())
the error i get is: 我得到的错误是:
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)
The interesting part is when i'm sending the request in my firefox browser, I'm typing the address 127.0.0.1/myAPI at the url line , i do get the json on screen and i can see it at the debug mode as a JSON {"hosts": [{"host": "127.0.0.1:4448"}]} 有趣的部分是,当我在firefox浏览器中发送请求时,我在url行输入地址127.0.0.1/myAPI ,我的确在屏幕上看到了json,并且可以在调试模式下将其作为JSON {“主机”:[{“主机”:“ 127.0.0.1:4448”}]}
so the json is valid.. 所以json是有效的。
at the debug i'm getting this page: 在调试中,我得到此页面:
<!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>
is any one have some way to solve it , or how can i read the json text straight from the respond object , or even look at that at the debug 有没有人有某种方法来解决它,或者我怎么能直接从response对象读取json文本,甚至在调试时查看它
I appreciate ant help , i'm trying for the least 3 days to figure it out Thanks 感谢蚂蚁的帮助,我尝试至少3天来解决这个问题,谢谢。
I think it's because of this simplejson.loads(response .read())
. 我认为是因为这个simplejson.loads(response .read())
。 You have a space between response and .read() 您在response和.read()之间有一个空格
Try the following: 请尝试以下操作:
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)
so it was my bad and the api was need a coockie. 所以这是我的坏事,而api需要一个库克。 after adding a cookie header i was receiving a correct JSON 添加Cookie标头后,我收到了正确的JSON
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.