繁体   English   中英

如何在嵌套字典中查找值?

[英]How to find value within a nested dictionary?

我有一个 API 响应返回以下 json:

{
    "statusCode": 200,
    "statusText": "Ok",
    "data": {
        "testId": "2278816_AklcAQ_E6B",
        "jsonUrl": "https://www.webpagetest.org/jsonResult.php?test=2278816_AklcAQ_E6B",
        "xmlUrl": "https://www.webpagetest.org/xmlResult/2278816_AklcAQ_E6B/",
        "userUrl": "https://www.webpagetest.org/result/2278816_AklcAQ_E6B/",
        "summaryCSV": "https://www.webpagetest.org/result/2278816_AklcAQ_E6B/page_data.csv",
        "detailCSV": "https://www.webpagetest.org/result/2278816_AklcAQ_E6B/requests.csv",
    },
}

然后我将其解析为字典:

json_data = json.loads(response.text)
  • 现在从这里我需要做的是找到键'testId'的值,并将其存储在一个变量中。

但是我已经尝试了很多东西,但我无法让它工作,我认为它的嵌套性质使它变得更难(这里是新手)

谢谢

尝试以下操作(假设您有 json 已在文件 temp.json 中写入)或来自有关此问题的请求:

import json

# Write code to read a json file and convert to Python dictionary
with open('temp.json') as f:
    json_data = json.load(f)

print(json_data["data"]["testId"])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM