【问题标题】:requests: filter a r.json() result请求:过滤 r.json() 结果
【发布时间】:2019-03-25 22:36:59
【问题描述】:
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 结果使其仅显示位置?

【问题讨论】:

    标签: python json python-requests


    【解决方案1】:

    在 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)
    

    【讨论】:

    • 谢谢!我知道我迟到了 2 年,但无论如何哈哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多