爲什麼我從這個代碼獲得解碼JSON與Python
ValueError: No JSON object could be decoded
:
import urllib.request,json
n = urllib.request.urlopen("http://graph.facebook.com/55")
d = json.loads(str(n.readall()))
完整的錯誤:
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
d= json.loads(str(n.readall()))
File "C:\Python33\lib\json\__init__.py", line 309, in loads
return _default_decoder.decode(s)
File "C:\Python33\lib\json\decoder.py", line 352, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python33\lib\json\decoder.py", line 370, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
的str(n.readall())
輸出:
'b\'{"id":"55","name":"Jillian Copeland","first_name":"Jillian","last_name":"Copeland","username":"JCoMD","gender":"female","locale":"en_US"}\''
也許b
正在拋棄它?
如果這是問題,我該如何將二進制流從readall
轉換爲字符串,而不是b
?
我想學習一點python,所以請記住這一點。
我在Windows中使用Python 3.3。
可能重複[?使用從JSON urlopen.read 「像字節對象」(http://stackoverflow.com/questions/10846112/use -byte-like-object-from-urlopen-read-with-json) –