要求:筛选r.json()结果

时间:2018-10-21 20:09:21

标签: python json python-requests

import requests

r = requests.get('https://REDACTED.zportal.nl/api/v3/appointments?user=~me&start=1542006900&end=1542009900&access_token=REDACTED')

print(r.json())

运行此代码时,我得到以下结果:

{
    "response": {
        "data": [
            {
                "locations": [
                    "018"
                ]
            }
        ]
    }
}

完整结果在这里:http://snippi.com/s/26v74yz

现在,我只想打印位置结果。如何过滤json结果,使其仅显示位置?

1 个答案:

答案 0 :(得分:0)

在Python中,JSON实际上就像dictionary一样工作,因此您可以使用方括号[]对其进行过滤。见下文。

import requests

r = requests.get('https://REDACTED.zportal.nl/api/v3/appointments?user=~me&start=1542006900&end=1542009900&access_token=REDACTED')

locations = r.json()["response"]["data"][0]["locations"]

print(locations)