Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

python - How do I get the value of a dict item within a list, within a dict?

How do I get the value of a dict item within a list, within a dict in Python? Please see the following code for an example of what I mean.

I use the following lines of code in Python to get data from an API.

res = requests.get('https://api.data.amsterdam.nl/bag/v1.1/nummeraanduiding/', params)
data = res.json()

data then returns the following Python dictionary:

{
    '_links': {
        'next': {
            'href': null
        },
        'previous': {
            "href": null
        },
        'self': {
            'href': 'https://api.data.amsterdam.nl/bag/v1.1/nummeraanduiding/'
        }
    },
    'count': 1,
    'results': [
        {
            '_display': 'Maple Street 99',
            '_links': {
                'self': {
                    'href': 'https://api.data.amsterdam.nl/bag/v1.1/nummeraanduiding/XXXXXXXXXXXXXXXX/'
                }
            },
            'dataset': 'bag',
            'landelijk_id': 'XXXXXXXXXXXXXXXX',
            'type_adres': 'Hoofdadres',
            'vbo_status': 'Verblijfsobject in gebruik'
        }
    ]
}

Using Python, how do I get the value for 'landelijk_id', represented by the twelve Xs?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should work:

>>> data['results'][0]['landelijk_id']
"XXXXXXXXXXXXXXXX"

You can just chain those [] for each child you need to access.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...