如何修复TypeError(type externalunifiedEvent的对象不是JSON序列化的对象)?

发布于 2025-02-09 22:56:51 字数 1306 浏览 2 评论 0 原文

我正在使用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)


I am retrieving a list of objects from an external API that I need to send as the payload to a webhook using post request in python.

The structure of the data returned from get_data() looks like this

[{"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))

it raises a TypeError('Object of type ExternalUnifiedEvent is not JSON serializable); if i remove the json.dumps() and just pass in the payload as the data, I'm getting TypeError('cannot unpack non-iterable 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2025-02-16 22:56:51

您的 get_data 函数正在返回 externalunifiedevent 对象,而不是列表或另一个 json serializable对象
您应该修复 get_data 函数,或将cude jsondecoder 提供给 dumps 方法作为 cls paramater:

json.dumps(payload, cls=YourCustomJSONEncoder)

您可以请参阅如何在

Your get_data function is returning an ExternalUnifiedEvent object instead of a list or another JSON serializable object.
You should fix your get_data function or provide a custom JSONDecoder to the dumps method as a cls paramater:

json.dumps(payload, cls=YourCustomJSONEncoder)

You can see how to make your custom JSON encoder class in the json python lib docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文