';类型集不可JSON序列化';但是数据是dict-python
我正在使用Microsoft的graph API发送电子邮件,因此我可以动态调整电子邮件的内容。我正在将电子邮件的有效负载设置为使用msg变量,该变量将根据一些输入进行调整。为了便于阅读,我将其设置为dict,如下所示:';类型集不可JSON序列化';但是数据是dict-python,json,python-3.x,dictionary,set,Json,Python 3.x,Dictionary,Set,我正在使用Microsoft的graph API发送电子邮件,因此我可以动态调整电子邮件的内容。我正在将电子邮件的有效负载设置为使用msg变量,该变量将根据一些输入进行调整。为了便于阅读,我将其设置为dict,如下所示: payload = { "message": { "subject": "Some Subject", "body": { "
payload = {
"message": {
"subject": "Some Subject",
"body": {
"contentType": "Text",
"content": msg
},
"toRecipients": [
{
"emailAddress": {
"somememail@email.com"
}
}
]
},
"saveToSentItemn": "false"
}
payload = "{\n \"message\": {\n \"subject\": \"Some Subject\",\n \"body\": {\n \"contentType\": \"Text\",\n \"content\": \"Some content.\"\n },\n \"toRecipients\": [\n {\n \"emailAddress\": {\n \"address\": \"someemail@email.com\"\n }\n }\n ]\n },\n \"saveToSentItems\": \"false\"\n}"
然后我将使用json.dumps(有效负载)将其转换为API所需的格式。但是,json.dumps会抛出一个错误:
TypeError: Object of type set is not JSON serializable
我不明白这是怎么一套。我不应该通过API发送电子邮件,当所有设置都是这样时,它可以正常工作:
payload = {
"message": {
"subject": "Some Subject",
"body": {
"contentType": "Text",
"content": msg
},
"toRecipients": [
{
"emailAddress": {
"somememail@email.com"
}
}
]
},
"saveToSentItemn": "false"
}
payload = "{\n \"message\": {\n \"subject\": \"Some Subject\",\n \"body\": {\n \"contentType\": \"Text\",\n \"content\": \"Some content.\"\n },\n \"toRecipients\": [\n {\n \"emailAddress\": {\n \"address\": \"someemail@email.com\"\n }\n }\n ]\n },\n \"saveToSentItems\": \"false\"\n}"
但是读书很可怕。有人看到我在有效载荷中的错误导致了错误吗?
在Python中,花括号可以用来初始化集合() 在有效负载中,
emailAddress
的值包装在{}
中:
{
"emailAddress": {
"somememail@email.com"
}
}
注:
固定的。。。真不敢相信我错过了。