我有flask
-服务。有时我可以在http
头上不带点就得到json
消息。在本例中,我试图解析来自request.data
的消息。但是request.data
中的字符串确实很难解析。它是一个二进制字符串,如下所示:
b'{\n "begindate": "2016-11-22", \n "enddate": "2016-11-22", \n "guids": ["6593062E-9030-B2BC-E63A-25FBB4723ECC", \n "5A9F8478-6673-428A-8E90-3AC4CD764543", \n "D8243BA1-0847-48BE-9619-336CB3B3C70C"]\n}'
当我试图使用json.loads()
时,我得到了以下错误:
TypeError: the JSON object must be str, not 'bytes'
转换为字符串(str()
)的函数也不起作用:
'b\'{\\n "begindate": "2016-11-22", \\n "enddate": "2016-11-22", \\n "guids": ["6593062E-9030-B2BC-E63A-25FBB4723ECC", \\n "5A9F8478-6673-428A-8E90-3AC4CD764543", \\n "D8243BA1-0847-48BE-9619-336CB3B3C70C"]\\n}\''
我用Python 3
。如何解析request.data
?
在传递给
json.loads
之前,只需decode
:Python 3.x明确区分了以下类型:
str
='...'
文本=一个Unicode字符序列(UTF-16或UTF-32,取决于Python的编译方式)bytes
=b'...'
文字=八位字节序列(0到255之间的整数)Link for more info
相关问题 更多 >
编程相关推荐