如何修复TypeError(type externalunifiedEvent的对象不是JSON序列化的对象)?
我正在使用Python中的POST请求来检索我需要将其作为有效载荷发送到Webhook的对象列表。
从 get_data()
返回的数据的结构看起来像这样
[{"eventId": 1,
"eventType": "test",
"properties": {
"property1": "value1",
"property2": "value2",
"property3": "value3",
}},
{"eventId": 2,
"eventType": "test",
"properties": {
"property1": "value1",
"property2": "value2",
"property3": "value3",
}},
{"eventId": 3,
"eventType": "test",
"properties": {
"property1": "value1",
"property2": "value2",
"property3": "value3",
}}]
headers = {"Content-Type": "application/json",}
payload = get_data()
response = requests.post(webhook_url, headers=headers, data=json.dumps(payload))
升级了 typeError('type externalunifiedEvent的对象不是JSON serializable)
;如果我删除 json.dumps()
,然后将有效载荷作为数据传递,我将获得 typeError('无法解开不可添加的backe> externalunifiedevent object')
)
def get_data():
try:
api_response = client_api.get()
data = []
for i in api_reponse:
e = {
"event_id": i.event_id
"event_type": i.event_type
"properties": i.properties
}
data.append(e)
return data
except Exception as e:
print(e)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
get_data
函数正在返回externalunifiedevent
对象,而不是列表或另一个 json serializable对象。您应该修复
get_data
函数,或将cudejsondecoder
提供给dumps
方法作为cls
paramater:您可以请参阅如何在。
Your
get_data
function is returning anExternalUnifiedEvent
object instead of a list or another JSON serializable object.You should fix your
get_data
function or provide a customJSONDecoder
to thedumps
method as acls
paramater:You can see how to make your custom JSON encoder class in the json python lib docs.