所以我遇到这个问题,我正在使用urllib来从服务器获取JSON响应,但是当我尝试将返回的b''
对象转换为JSON时,我收到一条错误消息{{1} }
这是我使用urllib2发出GET请求的Python代码:
"...is not JSON serializable"
以下是我从服务器获得的回复:
from urllib.request import Request, urlopen
from urllib.error import HTTPError
import json
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'application/json',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
file = Request('http://blahblahblah.com', headers=hdr)
try:
page = urlopen(file)
except HTTPError as e:
print(e)
print('')
return
content = page.read()
page.close()
print(page.info().get_content_charset()) # Returns 'utf-8'
print(content)
# print(json.dumps(content)) # Causes 'Not Serializable' Error
# print(content.decode('utf-8') # Causes 'UnicodeEncodeError' Error
我在其他地方读到我必须先将响应转换为b'{"game":{"id":1,"name":"Thief II: The Metal Age","slug":"thief-ii-the-metal-age","release_date":"2000-03-21","created_at":"2011-02-13 00:20:38 +0000","updated_at":"2016-03-15 19:41:25 +0000","alternative_names":[{"name":"Thief II: \xd0\xad\xd0\xbf\xd0\xbe\xd1\x85\xd0\xb0 \xd0\xbc\xd0\xb5\xd1\x82\xd0\xb0\xd0\xbb\xd0\xbb\xd0\xb0","comment":"Russian title"},{"name":"Dark Project II: The Metal Age","comment":"German title"},{"name":"Dark Project II: L\'Age de M\xc3\xa9tal","comment":"French title"}],"genres":[{"name":"Shooter"}],"themes":[{"name":"Action"},{"name":"Fantasy"},{"name":"Stealth"}],"rating":9.131189346313477,"release_dates":[{"platform_name":"Microsoft Windows","release_date":"2000-03-21"}],"companies":[{"id":4,"developer":false,"publisher":true,"name":"Eidos Interactive"},{"id":3,"developer":true,"publisher":false,"name":"Looking Glass Studios"},{"id":26,"developer":false,"publisher":true,"name":"Square Enix"}],"cover":{"url":"//res.cloudinary.com/igdb/image/upload/t_cover_small/qagoforxr6tofvpmgy9g.png","width":612,"height":650,"id":"qagoforxr6tofvpmgy9g"},"screenshots":[{"url":"//res.cloudinary.com/igdb/image/upload/t_screenshot_med/z0b9mqcqbtmnnxigekjc.jpg","title":"Microsoft Windows Title Screen","width":640,"height":480,"id":"z0b9mqcqbtmnnxigekjc"},{"url":"//res.cloudinary.com/igdb/image/upload/t_screenshot_med/puvydf5d6v0zirxfhzpg.jpg","title":"Microsoft Windows Ingame Screen","width":640,"height":480,"id":"puvydf5d6v0zirxfhzpg"}],"videos":[{"title":"Trailer","uid":"9C543B6uJ88"}]}}'
或使用str
编码,但当我尝试将utf-8
替换为print(content)
时,我得到以下错误:
print(content.decode('utf-8')
答案 0 :(得分:1)
服务器返回字节(我记得它是python 2中的字符串)我可以看到它的utf-8,所以你需要将它解码为unicode
例如,这有效:
print(json.loads(content.decode()))
这是一个链接,您可以在其中找到有关它的一些信息: https://docs.python.org/3.4/howto/unicode.html
您可能需要使用严格模式,请查看如何
答案 1 :(得分:0)
所以我最终弄明白了 - 问题是我的IDE(Pycharm 5.0)由于某种原因无法在IDE控制台中处理输出utf-8
,这导致了错误。解决方案是将以下代码行添加到我的Windows计算机中的pycharm.exe.vmpoptions
文件中:
-Dfile.encoding=UTF-8
当他们遇到类似的问题时,会向其他用户寻求相同的解决方案: