python - 如何在列表中附加 JSON 响应的值 (python)?

标签 python python-3.x http

我收到 request.post() 的响应,如下所示:

{'total': 3,
 'files': [{'fileName': 'abc.mp4', 'size': '123'},
           {'fileName': 'def.mp4', 'size': '456'},
           {'fileName': 'ghi.mp4', 'size': '789'}]
}

我只想要此响应中的文件名值并将其存储在 str 列表中。

我尝试了以下循环来执行相同的操作,但它显示了一些错误:

        fileNames = []
        for files in response.json()["files"]:
            fileNames.append(files["filename"])

我期望文件名列表,但出现一些错误

最佳答案

尝试使用:

fileNames = []
for files in response.json()["files"]:
    fileNames.append(files["fileName"])

我认为您写的是“filename”而不是“fileName”(“Name”大写)。

关于python - 如何在列表中附加 JSON 响应的值 (python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75181204/

相关文章:

python - 将命令从一个 docker 容器传递到另一个

angular - 覆盖 Angular httpclient get 请求上的基本 URL

c++ - CURL 不遵循 301 重定向,我需要做什么?

asp.net-mvc - 如何从 HttpAuthenticationContext 获取自定义 header 值

python - 如何在Python中比较两行并通过索引获取结果?

python - Django:使用对象作为字典键是否合理?

python - 更新 ttk.Notebook 中选项卡开关的框架

python-3.x - 如何将 s3 图像响应读取为对象

python - 在 python 文件夹中导入图表/结构(清理代码)

python - 对 Pandas 数据框上的函数进行多重处理