即使在使用decode('utf-8')後,我也會得到低於錯誤的錯誤。解碼UTF-8後,我得到TypeError:JSON對象必須是str,而不是'bytes'
TypeError: the JSON object must be str, not 'bytes'
現在我讀過相當多幾個人在面對3.X類似的問題,但他們大多利用解碼功能,這似乎並沒有爲我工作解決這個問題。任何人都可以幫我一下嗎?我是Python的初學者。
import urllib.request
import json
request = 'https://api.myjson.com/bins/56els'
response = urllib.request.urlopen(request)
obj = json.load(response)
str_response = response.readall().decode('utf-8')
print(obj)
使用'response'的兩行是相互獨立的;哪一個會引發錯誤?你可能只是想把'response.json()'的返回值賦給某個東西,替換一個或兩個當前行。 – chepner
'obj = json.load(response)'正在使用該錯誤。我也注意到我把print obj和str_response都解碼了,但是它沒有幫助解決這個錯誤。我試過你的方式,但也許我做的事情不對 – Trm