-3

我正在为我的应用程序集成 Stripe 付款。它工作正常 - Stripe 使用 JSON 响应。

我需要将其中一些字段存储在我的数据库中。如何检索特定字段,例如客户、国家和 default_card?

{
"account_balance": 0,
"cards": {
    "count": 1,
    "data": [
        {
            "address_city": null,
            "address_country": null,
            "address_line1": null,
            "address_line1_check": null,
            "address_line2": null,
            "address_state": null,
            "address_zip": null,
            "address_zip_check": null,
            "country": "US",
            "customer": "cus_2kZQDsySSBFBb3",
            "cvc_check": null,
            "exp_month": 5,
            "exp_year": 2015,
            "fingerprint": "Nf1jHB21DkK6brg6",
            "id": "card_102kZQ2wq4yXwSPaT66QP0ho",
            "last4": "4242",
            "name": null,
            "object": "card",
            "type": "Visa"
        }
    ],
    "object": "list",
    "url": "/v1/customers/cus_2kZQDsySSBFBb3/cards"
},
"created": 1381729976,
"default_card": "card_102kZQ2wq4yXwSPaT66QP0ho",
"delinquent": false,
"description": "rohit.fantasy@yahoo.com",
"discount": null,
"email": null,
"id": "cus_2kZQDsySSBFBb3",
"livemode": false,
"metadata": {},
"object": "customer",
"subscription": null
}
4

1 回答 1

2
import json

s = '''{ "account_balance": 0, "cards": { "count": 1, "data": [ { "address_city": null, "address_country": null, "address_line1": null, "address_line1_check": null, "address_line2": null, "address_state": null, "address_zip": null, "address_zip_check": null, "country": "US", "customer": "cus_2kZQDsySSBFBb3", "cvc_check": null, "exp_month": 5, "exp_year": 2015, "fingerprint": "Nf1jHB21DkK6brg6", "id": "card_102kZQ2wq4yXwSPaT66QP0ho", "last4": "4242", "name": null, "object": "card", "type": "Visa" } ], "object": "list", "url": "/v1/customers/cus_2kZQDsySSBFBb3/cards" }, "created": 1381729976, "default_card": "card_102kZQ2wq4yXwSPaT66QP0ho", "delinquent": false, "description": "rohit.fantasy@yahoo.com", "discount": null, "email": null, "id": "cus_2kZQDsySSBFBb3", "livemode": false, "metadata": {}, "object": "customer", "subscription": null }'''

parsedJson = json.loads(s) 

country = parsedJson['cards']['data'][0]['country']

您可以检查parsedJson结构并相应地检索其他值。

于 2013-10-14T06:06:55.307 回答