簡體   English   中英

嘗試從Webhook解析JSON數據,但返回錯誤

[英]Trying to parse JSON data from webhook but returns error

我能夠很好地接收到Webhooks,但是當我嘗試添加代碼以讀取數據時,它將返回錯誤。 我解析不正確嗎?

@require_POST
@csrf_exempt
def webhook(request):
    print("webhook kaldt")
    event_json = json.loads(request.body)
    status = event_json['status']
    print(status)
    return HttpResponse(status=200)

如果我刪除status變量和打印行,則一切正常。 就在我嘗試解析數據時。

來自event_json的數據如下所示

{“ created”:1326853478,“ livemode”:false,“ id”:“ charge.succeeded_00000000000000”,“ type”:“ charge.succeeded”,“ object”:“ event”,“ request”:null,“ pending_webhooks” :1,1,“ api_version”:“ 2019-03-14”,“ data”:{“ object”:{“ id”:“ ch_00000000000000”,“ object”:“費用”,“ amount”:100,“ amount_refunded” :0,“應用程序”:空,“ application_fee”:空,“ application_fee_amount”:空,“ balance_transaction”:“ txn_00000000000000”,“ billing_details”:{“地址”:{“ city”:空,“ country”:“ DK”,“ line1”:null,“ line2”:null,“ postal_code”:null,“ state”:null},“ email”:null,“ name”:“ qwewqwq”,“ phone”:null},“已捕獲”:錯誤,“創建”:1553170537,“貨幣”:“ dkk”,“客戶”:空,“描述”:“我的首次測試費用(為API文檔創建)”,“目的地”:空,“爭議” “:null,” failure_code“:null,” failure_message“:null,” fraud_details“:{},”發票“:null,” livemode“:false,”元數據“:{},” on_behalf_of“:null,”順序“:null,”結果“:null,”已付費“:true,” payment_intent“:null,” p ayment_method_details“:{”卡片“:{”品牌“:”簽證“,”支票“:{” address_line1_check“:空,” address_postal_code_check“:空,” cvc_check“:”通過“},”國家“:”美國“ ,“ exp_month”:1,“ exp_year”:2022,“ fingerprint”:“ JKbTj7sZqbPMQJ9p”,“ funding”:“ credit”,“ last4”:“ 4242”,“ three_d_secure”:null,“ wallet”:null}, “ type”:“ card”},“ receipt_email”:null,“ receipt_number”:null,“ receipt_url”:“ https://pay.stripe.com/receipts/acct_1E4YZiB2MQ8LEk6k/ch_1EGPo5B2Mpt8LEk6k47xo3CcZb1KExE3CKb1KExE3CJB2K8eJE8EJ4K6E8E7E7E7E7C4 ,“ refunds”:{“對象”:“列表”,“數據”:[],“ has_more”:假,“ total_count”:0,“ url”:“ / v1 / charges / ch_1EGPo5B2MQ8LEk6k47xo3CcZ / refunds”},“評論”:null,“運輸”:null,“來源”:{“ id”:“ card_00000000000000”,“對象”:“ card”,“ address_city”:null,“ address_country”:“ DK”,“ address_line1”: null,“ address_line1_check”:null,“ address_line2”:null,“ address_state”:null,“ address_zip”:null,“ address_zip_check”:null,“ brand”:“ Visa”,“ country”:“ US”,“ custome r“:” cus_00000000000000“,” cvc_check“:”通過“,” dynamic_last4“:空,” exp_month“:1,” exp_year“:2022,”指紋“:” JKbTj7sZqbPMQJ9p“,”資金“:”信用“,” last4“:” 4242“,”元數據“:{},”名稱“:” qwewqwq“,” tokenization_method“:空},” source_transfer“:空,” statement_descriptor“:空,” status“:”成功“,” transfer_data“:空,” transfer_group“:空}}}

如果您美化json,您會看到根對象中沒有'status'屬性

你可以試試

event_json['data']['object']['status']

格式化的json

{
    "created": 1326853478,
    "livemode": false,
    "id": "charge.succeeded_00000000000000",
    "type": "charge.succeeded",
    "object": "event",
    "request": null,
    "pending_webhooks": 1,
    "api_version": "2019-03-14",
    "data": {
        "object": {
            "id": "ch_00000000000000",
            "object": "charge",
            "amount": 100,
            "amount_refunded": 0,
            "application": null,
            "application_fee": null,
            "application_fee_amount": null,
            "balance_transaction": "txn_00000000000000",
            "billing_details": {
                "address": {
                    "city": null,
                    "country": "DK",
                    "line1": null,
                    "line2": null,
                    "postal_code": null,
                    "state": null
                },
                "email": null,
                "name": "qwewqwq",
                "phone": null
            },
            "captured": false,
            "created": 1553170537,
            "currency": "dkk",
            "customer": null,
            "description": "My First Test Charge (created for API docs)",
            "destination": null,
            "dispute": null,
            "failure_code": null,
            "failure_message": null,
            "fraud_details": {},
            "invoice": null,
            "livemode": false,
            "metadata": {},
            "on_behalf_of": null,
            "order": null,
            "outcome": null,
            "paid": true,
            "payment_intent": null,
            "payment_method_details": {
                "card": {
                    "brand": "visa",
                    "checks": {
                        "address_line1_check": null,
                        "address_postal_code_check": null,
                        "cvc_check": "pass"
                    },
                    "country": "US",
                    "exp_month": 1,
                    "exp_year": 2022,
                    "fingerprint": "JKbTj7sZqbPMQJ9p",
                    "funding": "credit",
                    "last4": "4242",
                    "three_d_secure": null,
                    "wallet": null
                },
                "type": "card"
            },
            "receipt_email": null,
            "receipt_number": null,
            "receipt_url": "https://pay.stripe.com/receipts/acct_1E4YZiB2MQ8LEk6k/ch_1EGPo5B2MQ8LEk6k47xo3CcZ/rcpt_EjtbkjCrHE8bfSS0Xxa5iW3UBNjqK7C",
            "refunded": false,
            "refunds": {
                "object": "list",
                "data": [],
                "has_more": false,
                "total_count": 0,
                "url": "/v1/charges/ch_1EGPo5B2MQ8LEk6k47xo3CcZ/refunds"
            },
            "review": null,
            "shipping": null,
            "source": {
                "id": "card_00000000000000",
                "object": "card",
                "address_city": null,
                "address_country": "DK",
                "address_line1": null,
                "address_line1_check": null,
                "address_line2": null,
                "address_state": null,
                "address_zip": null,
                "address_zip_check": null,
                "brand": "Visa",
                "country": "US",
                "customer": "cus_00000000000000",
                "cvc_check": "pass",
                "dynamic_last4": null,
                "exp_month": 1,
                "exp_year": 2022,
                "fingerprint": "JKbTj7sZqbPMQJ9p",
                "funding": "credit",
                "last4": "4242",
                "metadata": {},
                "name": "qwewqwq",
                "tokenization_method": null
            },
            "source_transfer": null,
            "statement_descriptor": null,
            "status": "succeeded",
            "transfer_data": null,
            "transfer_group": null
        }
    }
}```

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM