TypeError:b'1'不是JSON可序列化的 [英] TypeError: b'1' is not JSON serializable
本文介绍了TypeError:b'1'不是JSON可序列化的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将POST请求作为JSON发送。
I am trying to send a POST request as JSON.
*电子邮件变量的类型为bytes
*email variable is of type "bytes"
def request_to_SEND(email, index):
url = "....."
data = {
"body": email.decode('utf-8'),
"query_id": index,
"debug": 1,
"client_id": "1",
"campaign_id": 1,
"meta": {"content_type": "mime"}
}
headers = {'Content-type': 'application/json'}
try:
response = requests.post(url, data=json.dumps(data), headers=headers)
except requests.ConnectionError:
sys.exit()
return response
我收到错误:
File "C:\Python34\lib\json\encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'1' is not JSON serializable
C你能不能告诉我,我做错了什么?
Could you please tell me what is it that I am doing wrong?
推荐答案
这种情况正在发生,因为你传递的是 bytes
数据中的对象
dict( b'1'
,具体而言),可能是 index
的值。在 json.dumps
可以使用之前,您需要将其解码为 str
对象:
This is happening because you're passing a bytes
object in the data
dict (b'1'
, specifically), probably as the value of index
. You need to decode it to a str
object before json.dumps
can work with it:
data = {
"body": email.decode('utf-8'),
"query_id": index.decode('utf-8'), # decode it here
"debug": 1,
"client_id": "1",
"campaign_id": 1,
"meta": {"content_type": "mime"}
}
这篇关于TypeError:b'1'不是JSON可序列化的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文