stripe:如何将stripe模型对象转换成JSON,得到完整的层次化数据?

bybem2ql  于 2022-12-15  发布在  其他
关注(0)|答案(2)|浏览(136)

如何将条带模型对象转换为JSON以在客户端接收完整的层次化数据?

stripeCustomer = stripe.Customer.retrieve(<stripe customer id>)
sendResponseToClient(stripeCustomer)

我在客户端只接收到第一级数据作为json,来自Stripe JSON对象的第二级数据未格式化。
JSON数据示例:(客户端未接收到二级数据)

Customer customer id=cus_DTWEPsfrHx3ikZ at 0x00000a> JSON: {
      "id": "cus_DTWEPsfrHx3ikZ",
      "object": "customer",
      "account_balance": 0,
      "created": 1535093686,
      "currency": "usd",
      "default_source": null,
      "delinquent": false,
      "description": null,
      "discount": null,
      "email": "rakesh16+test9@gmail.com",
      "invoice_prefix": "E91FF30",
      "livemode": false,
      "metadata": {
      },
      "shipping": null,
      "sources": {
        "object": "list",
        "data": [

        ],
        "has_more": false,
        "total_count": 0,
        "url": "/v1/customers/cus_DTWEPsfrHx3ikZ/sources"
      },
      "subscriptions": {
        "object": "list",
        "data": [
          {
            "id": "sub_DTWEALN3urFael",
            "object": "subscription",
            "application_fee_percent": null,
            "billing": "charge_automatically",
            "billing_cycle_anchor": 1535093688,
            "cancel_at_period_end": false,
            "canceled_at": null,
            "created": 1535093688,
            "current_period_end": 1537772088,
            "current_period_start": 1535093688,
            "customer": "cus_DTWEPsfrHx3ikZ",
            "days_until_due": null,
            "discount": null,
            "ended_at": null,
            "items": {
              "object": "list",
              "data": [
                {
                  "id": "si_DTWEuZaU4pw9Cv",
                  "object": "subscription_item",
                  "created": 1535093688,
                  "metadata": {
                  },
                  "plan": {
                    "id": "plan_free",
                    "object": "plan",
                    "active": true,
                    "aggregate_usage": null,
                    "amount": 0,
                    "billing_scheme": "per_unit",
                    "created": 1535008667,
                    "currency": "usd",
                    "interval": "month",
                    "interval_count": 1,
                    "livemode": false,
                    "metadata": {
                    },
                    "nickname": "free",
                    "product": "prod_DT8B8auk3CRNdw",
                    "tiers": null,
                    "tiers_mode": null,
                    "transform_usage": null,
                    "trial_period_days": null,
                    "usage_type": "licensed"
                  },
                  "quantity": 1,
                  "subscription": "sub_DTWEALN3urFael"
                }
              ],
              "has_more": false,
              "total_count": 1,
              "url": "/v1/subscription_items?subscription=sub_DTWEALN3urFael"
            },
            "livemode": false,
            "metadata": {
            },
            "plan": {
              "id": "plan_free",
              "object": "plan",
              "active": true,
              "aggregate_usage": null,
              "amount": 0,
              "billing_scheme": "per_unit",
              "created": 1535008667,
              "currency": "usd",
              "interval": "month",
              "interval_count": 1,
              "livemode": false,
              "metadata": {
              },
              "nickname": "free",
              "product": "prod_DT8B8auk3CRNdw",
              "tiers": null,
              "tiers_mode": null,
              "transform_usage": null,
              "trial_period_days": null,
              "usage_type": "licensed"
            },
            "quantity": 1,
            "start": 1535093688,
            "status": "active",
            "tax_percent": null,
            "trial_end": null,
            "trial_start": null
          }
        ],
        "has_more": false,
        "total_count": 1,
        "url": "/v1/customers/cus_DTWEPsfrHx3ikZ/subscriptions"
      },
      "tax_info": null,
      "tax_info_verification": null
    }
92dk7w1h

92dk7w1h1#

您可以使用以下技术将返回的Stripe对象模型转换为JSON,以忽略不可序列化的字段:

stripeObject = stripe.SomeAPICall(...)
jsonEncoded = json.dumps(stripeObject, default=lambda o: '<not serializable>')
pythonDict = json.loads(jsonEncoded)
ijxebb2r

ijxebb2r2#

您可以使用to_dict()API将条带模型对象转换为字典格式,然后最终转换为JSON字符串。

相关问题