javascript - 尝试使用 JSON 的点表示法从 Stripe API 获取特定键值时遇到问题

标签 javascript json stripe-payments

我正在从测试发票的 Stripe API 中提取数据。我正在尝试使用点表示法缩小范围以提取特定的键值对,但是我在从特定键值提取数据时遇到了麻烦,因为它似乎比其余数据嵌套得更深(不确定这是否正确)术语)。

我尝试从中提取数据的数据来自 period: end:period: start: 大致从第 44-46 行开始。

如果我在代码中使用点表示法,则当键值对位于同一行时,它会起作用,例如 sheet.getRange(3,2).setValue([content.amount_due]);。但似乎“period: end”和“period: start”嵌套得更深。

我尝试执行类似 [content.lines.data.period.end] 的操作,但没有成功。它返回此错误:TypeError:无法从未定义中读取属性“end”。

以下是从 API 中提取的完整数据(注意:“xxxxxxxxxxxxxxxx” 表示已隐藏的数据):

  ""id"": ""in_xxxxxxxxxxxxxxxx"",
  ""object"": ""invoice"",
  ""amount_due"": 800,
  ""amount_paid"": 800,
  ""amount_remaining"": 0,
  ""application_fee"": null,
  ""attempt_count"": 1,
  ""attempted"": true,
  ""auto_advance"": false,
  ""billing"": ""charge_automatically"",
  ""billing_reason"": ""subscription_cycle"",
  ""charge"": ""ch_xxxxxxxxxxxxxxxx"",
  ""closed"": true,
  ""created"": 1552077120,
  ""currency"": ""usd"",
  ""custom_fields"": null,
  ""customer"": ""cus_xxxxxxxxxxxxxxxx"",
  ""date"": 1552077120,
  ""default_payment_method"": null,
  ""default_source"": null,
  ""description"": null,
  ""discount"": null,
  ""due_date"": null,
  ""ending_balance"": 0,
  ""finalized_at"": 1552080770,
  ""footer"": null,
  ""forgiven"": false,
  ""hosted_invoice_url"": ""https://pay.stripe.com/invoice/invst_xxxxxxxxxxxxxxxx"",
  ""invoice_pdf"": ""https://pay.stripe.com/invoice/invst_xxxxxxxxxxxxxxxx/pdf"",
  ""lines"": {
    ""object"": ""list"",
    ""data"": [
      {
        ""id"": ""sub_xxxxxxxxxxxxxxxx"",
        ""object"": ""line_item"",
        ""amount"": 800,
        ""currency"": ""usd"",
        ""description"": null,
        ""discountable"": true,
        ""livemode"": false,
        ""metadata"": {
        },
        ""period"": {
          ""end"": 1554755515,
          ""start"": 1552077115
        },
        ""plan"": {
          ""id"": ""xxxxxxxxxxxxxxxx"",
          ""object"": ""plan"",
          ""active"": true,
          ""aggregate_usage"": null,
          ""amount"": 400,
          ""billing_scheme"": ""per_unit"",
          ""created"": 1407529859,
          ""currency"": ""usd"",
          ""interval"": ""month"",
          ""interval_count"": 1,
          ""livemode"": false,
          ""metadata"": {
          },
          ""name"": ""sdf"",
          ""nickname"": null,
          ""product"": ""prod_xxxxxxxxxxxxxxxx"",
          ""statement_description"": null,
          ""statement_descriptor"": null,
          ""tiers"": null,
          ""tiers_mode"": null,
          ""transform_usage"": null,
          ""trial_period_days"": null,
          ""usage_type"": ""licensed""
        },
        ""proration"": false,
        ""quantity"": 2,
        ""subscription"": null,
        ""subscription_item"": ""si_xxxxxxxxxxxxxxxx"",
        ""type"": ""subscription"",
        ""unique_line_item_id"": ""sli_xxxxxxxxxxxxxxx""
      }
    ],
    ""has_more"": false,
    ""total_count"": 1,
    ""url"": ""/v1/invoices/in_xxxxxxxxxxxxxxxx/lines""
  },
  ""livemode"": false,
  ""metadata"": {
  },
  ""next_payment_attempt"": null,
  ""number"": ""77E3C02-0025"",
  ""paid"": true,
  ""payment"": ""ch_1xxxxxxxxxxxxxxxx"",
  ""payment_intent"": ""pi_xxxxxxxxxxxxxxxx"",
  ""period_end"": 1552077115,
  ""period_start"": 1549657915,
  ""receipt_number"": ""2258-2679"",
  ""starting_balance"": 0,
  ""statement_description"": null,
  ""statement_descriptor"": null,
  ""status"": ""paid"",
  ""status_transitions"": {
    ""finalized_at"": 1552080770,
    ""marked_uncollectible_at"": null,
    ""paid_at"": 1552080772,
    ""voided_at"": null
  },
  ""subscription"": ""sub_xxxxxxxxxxxxxxxx"",
  ""subtotal"": 800,
  ""tax"": null,
  ""tax_percent"": null,
  ""total"": 800,
  ""webhooks_delivered_at"": 1552077123
}"```


  [1]: https://stripe.com/docs/api/invoices/retrieve

最佳答案

data 是一个数组,请尝试 content.lines.data[0].period.end

关于javascript - 尝试使用 JSON 的点表示法从 Stripe API 获取特定键值时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55750914/

相关文章:

stripe-payments - Stripe 连接直接充电与意图

php - 在 WooCommerce 中以编程方式对保存的 Stripe 卡收费

使用 WebWorker 的 JavaScript DoS 攻击

javascript - 如何让 Vue Sortable 在桌面上工作?

javascript - 通过鼠标移动绘制的矩形会在鼠标移动时不断绘制自身,同时显示之前的矩形

javascript - 前端连接到后端

javascript - const 已经在 ES6 开关 block 中声明

javascript - 在 ReactJS 中用 Jquery 替换 JSON 时未定义“$”

Python 尝试附加 JSON 文件时出错

reactjs - 请将 'apiKey' 或 'stripe' 传递给 StripeProvider