Python 3.4.3 json.dumps() "is not JSON serializable" 从字节转换时出错
Python 3.4.3 json.dumps() "is not JSON serializable" Error When Converting From Bytes
所以我在使用 urllib 从服务器获取 JSON 响应时遇到了这个问题,但是当我尝试将返回的 b''
对象转换为 JSON 时,我收到一条错误消息 "...is not JSON serializable"
这是我的 Python 使用 urllib2 发出 GET 请求的代码:
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.decode('utf-8')
替换 print(content)
时,我收到以下错误:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 231-235: character maps to <undefined>
服务器 returns 字节(我记得它是 python 2 中的字符串)我可以看到它是 utf-8,所以你需要将它解码为 unicode
例如这个有效:
print(json.loads(content.decode()))
这是一个 link,您可以在其中找到一些相关信息:
https://docs.python.org/3.4/howto/unicode.html
您可能需要使用严格模式,查看如何
所以我最终弄明白了 - 问题是我的 IDE (Pycharm 5.0) 由于某种原因无法处理 IDE 控制台中的输出 utf-8
,这导致了错误。解决方案是将以下代码行添加到我的 windows 计算机中的 pycharm.exe.vmpoptions
文件中:
-Dfile.encoding=UTF-8
感谢其他用户在遇到类似问题时找到相同的解决方案:
Source:
所以我在使用 urllib 从服务器获取 JSON 响应时遇到了这个问题,但是当我尝试将返回的 b''
对象转换为 JSON 时,我收到一条错误消息 "...is not JSON serializable"
这是我的 Python 使用 urllib2 发出 GET 请求的代码:
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.decode('utf-8')
替换 print(content)
时,我收到以下错误:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 231-235: character maps to <undefined>
服务器 returns 字节(我记得它是 python 2 中的字符串)我可以看到它是 utf-8,所以你需要将它解码为 unicode
例如这个有效:
print(json.loads(content.decode()))
这是一个 link,您可以在其中找到一些相关信息: https://docs.python.org/3.4/howto/unicode.html
您可能需要使用严格模式,查看如何
所以我最终弄明白了 - 问题是我的 IDE (Pycharm 5.0) 由于某种原因无法处理 IDE 控制台中的输出 utf-8
,这导致了错误。解决方案是将以下代码行添加到我的 windows 计算机中的 pycharm.exe.vmpoptions
文件中:
-Dfile.encoding=UTF-8
感谢其他用户在遇到类似问题时找到相同的解决方案:
Source: