从Python列表中获取dict

2024-10-02 08:18:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个JSON,看起来像这样:

"payment_methods": [{
    "payment_type": "CC",
    "cardSubType": "AX",
    "card_number": "377777684182882",
    "expiration_month": "01",
    "expiration_year": "2018",
    "security_code": "",
    "cvv2": "",
    "name": {
        "first_name": "John",
        "last_name": "Smith"
    },
    "billing_address": {
        "country": "United States",
        "address_line1": "666 SUNSET BLVD",
        "address_line2": null,
        "city": "Anaheim",
        "state": "California",
        "zip_code": "92808",
        "phone": "0123456789"
    },
    "payment_amount": "",
    "payment_percentage": ""
}]

需要做的是将billing_address保存到一个变量中,以便以后使用该dict中的值。 billing_address in payment_methods行得通吗?你知道吗

有没有更好的方法?你知道吗


Tags: namejsonnumberaddresstypecodepaymentax
3条回答

所以你想获得billing\u address属性?你知道吗

import json

#Fix the null, normalize the json since is not a valid json
null = None
data = json.dumps({"payment_methods": [{
    "payment_type": "CC",
    "cardSubType": "AX",
    "card_number": "377777684182882",
    "expiration_month": "01",
    "expiration_year": "2018",
    "security_code": "",
    "cvv2": "",
    "name": {
        "first_name": "John",
        "last_name": "Smith"
    },
    "billing_address": {
        "country": "United States",
        "address_line1": "666 SUNSET BLVD",
        "address_line2": null,
        "city": "Anaheim",
        "state": "California",
        "zip_code": "92808",
        "phone": "0123456789"
    },
    "payment_amount": "",
    "payment_percentage": ""
}]})

data = json.loads(data)

billing_address = data["payment_methods"][0]["billing_address"]

print billing_address

您有一个JSON字符串,所以将其解析到Python字典中。(几乎有效的)字符串表示一个字典,但它需要包含在{}中。假设json字符串是从其他API返回的,可以使用^{}这样做:

json_string = '{{{}}}'.format('''"payment_methods": [{
    "payment_type": "CC",
    "cardSubType": "AX",
    "card_number": "377777684182882",
    "expiration_month": "01",
    "expiration_year": "2018",
    "security_code": "",
    "cvv2": "",
    "name": {
        "first_name": "John",
        "last_name": "Smith"
    },
    "billing_address": {
        "country": "United States",
        "address_line1": "666 SUNSET BLVD",
        "address_line2": null,
        "city": "Anaheim",
        "state": "California",
        "zip_code": "92808",
        "phone": "0123456789"
    },
    "payment_amount": "",
    "payment_percentage": ""
}]''')

现在可以使用^{}模块将字符串解析到字典中,并通过访问字典中相应的键和列表索引来获取帐单地址的详细信息。假设您只需要(可能有许多)付款方式列表中第一种付款方式的帐单详细信息:

import json
from pprint import pprint

payment_methods = json.loads(json_string)['payment_methods']
billing_address = payment_methods[0]['billing_address']

>>> pprint(billing_address)
{u'address_line1': u'666 SUNSET BLVD',
 u'address_line2': None,
 u'city': u'Anaheim',
 u'country': u'United States',
 u'phone': u'0123456789',
 u'state': u'California',
 u'zip_code': u'92808'}

我会把这些分成两本字典。带有地址的第二个字典将存储在变量billing_address

它看起来是这样的:

payment_methods = {
                  "payment_type": "CC",
                  "cardSubType": "AX",
                  "card_number": "377777684182882",
                  "expiration_month": "01",
                  "expiration_year": "2018",
                  "security_code": "",
                  "cvv2": "",
                  "first_name": "John",
                  "last_name": "Smith",
                  "payment_amount": "",
                  "payment_percentage": ""
                  }
billing_address = {
                      "country": "United States",
                      "address_line1": "666 SUNSET BLVD",
                      "address_line2": "null",
                      "city": "Anaheim",
                      "state": "California",
                      "zip_code": "92808",
                      "phone": "0123456789"
                    }

相关问题 更多 >

    热门问题