【问题标题】:how to find a key/value in this json response using python [closed]如何使用python在这个json响应中找到一个键/值[关闭]
【发布时间】:2022-08-18 23:12:29
【问题描述】:

使用 python,我试图在以下响应中访问 \"load Time\": \"value\":

response = requests.get(f\'https://www.webpagetest.org/jsonResult.phptest=220816_AiDcTD_FHD&average=0&standard=0&requests=0&lighthouse=0&pretty=1\').json()
    \"data\": {
        \"id\": \"220816_AiDcTD_FHD\",
        \"url\": \"https:\\/\\/www.ghdhair.com\",
        \"summary\": \"https:\\/\\/www.webpagetest.org\\/results.php?test=220816_AiDcTD_FHD\",
        \"testUrl\": \"https:\\/\\/www.ghdhair.com\",
        \"location\": \"ec2-us-east-1:Chrome\",
        \"successfulRVRuns\": 1,
        \"average\": {
            \"firstView\": {
                \"loadTime\": 7435,
                \"docTime\": 7435,
                \"fullyLoaded\": 13841,
                \"bytesOut\": 170730,
                \"bytesOutDoc\": 127136,

它继续。

我想从中访问 \"loadTime\" 值。我怎样才能做到这一点?

谢谢

  • response[\'data\'][\'average\'][\'firstView\'][\'loadTime\']…?!

标签: python json


【解决方案1】:

假设您已经将 json 数据加载到response,那么我们可以沿着字典和键的链访问loadTime

print(response['data']['average']['firstView']['loadTime'])

【讨论】:

  • 我试过这个,但得到 KeyError: 'average'
猜你喜欢
  • 2021-12-15
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
相关资源
最近更新 更多