无法在json正文中打印特定字段

时间:2019-04-04 22:04:46

标签: python json

我需要在JSON正文中打印名为"payment_intent"的字段。我目前无法这样做。似乎我在请求正确的参数。

我已经尝试过

payment_intent = event['data']['object']['payment_intent']

我的JSON正文:

{
  "object": {
    "id": "",
    "object": "payment_intent",
    "amount": 100,
    "amount_capturable": 0,
    "amount_received": 100,
    "application": null,
    "application_fee_amount": null,
    "canceled_at": null,
    "cancellation_reason": null,
    "capture_method": "automatic",
    "charges": {
      "object": "list",
      "data": [
        {
          "id": "",
          "object": "charge",
          "amount": 100,
          "amount_refunded": 0,
          "application": null,
          "application_fee": null,
          "application_fee_amount": null,
          "balance_transaction": "",
          "billing_details": {
            "address": {
              "city": null,
              "country": "",
              "line1": null,
              "line2": null,
              "postal_code": null,
              "state": null
            },
            "email": "",
            "name": "",
            "phone": null
          },
          "captured": true,
          "created": 1554414723,
          "currency": "usd",
          "customer": "",
          "description": null,
          "destination": null,
          "dispute": null,
          "failure_code": null,
          "failure_message": null,
          "fraud_details": {
          },
          "invoice": null,
          "livemode": true,
          "metadata": {
          },
          "on_behalf_of": null,
          "order": null,
          "outcome": {
            "network_status": "approved_by_network",
            "reason": null,
            "risk_level": "normal",
            "seller_message": "Payment complete.",
            "type": "authorized"
          },
          "paid": true,
          "payment_intent": "pi_1ELdTCB2MQ8LEk6k2PTkpRd0",
          "payment_method_details": {
            "card": {
              "brand": "visa",
              "checks": {
                "address_line1_check": null,
                "address_postal_code_check": null,
                "cvc_check": "pass"
              },
              "country": "DK",
              "exp_month": ,
              "exp_year": ,
              "fingerprint": "",
              "funding": "",
              "last4": "",
              "three_d_secure": null,
              "wallet": null
            },
            "type": "card"
          },
          "receipt_email": "",
          "receipt_number": "1308-6739",
          "receipt_url": "",
          "refunded": false,
          "refunds": {
            "object": "list",
            "data": [
            ],
            "has_more": false,
            "total_count": 0,
            "url": "/v1/charges/ch_1ELdTbB2MQ8LEk6kOQQd5T9n/refunds"
          },
          "review": null,
          "shipping": null,
          "source": {
            "id": "",
            "object": "source",
            "amount": null,
            "card": {
              "exp_month": ,
              "exp_year": ,
              "last4": "",
              "country": "",
              "brand": "",
              "cvc_check": "",
              "funding": "debit",
              "fingerprint": "",
              "three_d_secure": "optional",
              "name": null,
              "address_line1_check": null,
              "address_zip_check": null,
              "tokenization_method": null,
              "dynamic_last4": null
            },
            "client_secret": "",
            "created": 15,
            "currency": null,
            "customer": "",
            "flow": "none",
            "livemode": true,
            "metadata": {
            },
            "owner": {
              "address": {
                "city": null,
                "country": "DK",
                "line1": null,
                "line2": null,
                "postal_code": null,
                "state": null
              },
              "email": "",
              "name": "",
              "phone": null,
              "verified_address": null,
              "verified_email": null,
              "verified_name": null,
              "verified_phone": null
            },
            "statement_descriptor": null,
            "status": "chargeable",
            "type": "card",
            "usage": "reusable"
          },
          "source_transfer": null,
          "statement_descriptor": null,
          "status": "succeeded",
          "transfer_data": null,
          "transfer_group": null
        }
      ],
      "has_more": false,
      "total_count": 1,
      "url": "/v1/charges?payment_intent=pi_1ELdTCB2MQ8LEk6k2PTkpqweEwwRd0"
    },
    "confirmation_method": "automatic",
    "created": 1554414698,
    "currency": "usd",
    "customer": "",
    "description": null,
    "last_payment_error": null,
    "livemode": true,
    "metadata": {
    },
    "next_action": null,
    "on_behalf_of": null,
    "payment_method_types": [
      "card"
    ],
    "receipt_email": null,
    "review": null,
    "shipping": null,
    "source": "",
    "statement_descriptor": null,
    "status": "succeeded",
    "transfer_data": null,
    "transfer_group": null
  }
}

1 个答案:

答案 0 :(得分:0)

您即将请求正确的参数,但是您需要从外到内逐步浏览数据包,直到到达payment_intent参数为止。

假设event是您的JSON对象的名称,请尝试使用:

payment_intent = event['object']['charges']['data'][0]['payment_intent']

打印变量应为实际数据字段

  

pi_1ELdTCB2MQ8LEk6k2PTkpRd0