python - Stripe : How to convert stripe model object into JSON to get complete hierarchical data?

标签 python json stripe-payments

如何将 strip 模型对象转换为JSON,以便在客户端接收完整的分层数据?

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

我在客户端仅接收 json 形式的第一级数据,来自 Stripe JSON 对象的第二级数据未格式化。

JSON 数据示例:(客户端未收到第 2 级数据,)

 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
    }

最佳答案

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

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

关于python - Stripe : How to convert stripe model object into JSON to get complete hierarchical data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52043797/

相关文章:

python - 有没有更短的方法来使用列表?

javascript - === 和 == 如何以不同方式处理 null 比较?

android - 在 android 中进行 Stripe 集成并创建 token

stripe-payments - Stripe 重定向 URI - url 中的动态数字

python - PyCuda mem_alloc 初始化错误

python - 如何创建可以向类添加多个方法的类装饰器?

python - 如何创建一个在每次设置事件时运行函数的事件?

java - 意外 token : for JSON string in Javascript

json - 在 Go 中表示 JSON 策略

javascript - 在 Parse.com Cloud Code 和 Stripe 中通过 JSON 进行解析