首页 > 解决方案 > 使用 Python 从 json 文件中读取特定值

问题描述

我从 curl 请求中获取 JSON 文件,我想从中读取特定值。假设我有一个 JSON 文件,如下所示。如何将“result_count”值插入变量?

JSON 文件的示例

目前,在得到 curl 的响应后,我正在将 JSON 对象写入这样的 txt 文件中。

json_response = connect_to_endpoint(url, headers)

f.write(json.dumps(json_response, indent=4, sort_keys=True))

标签: pythonjsoncurl

解决方案


json_response 不是 JSON 内容(JSON 是格式化字符串),而是 a python dict,您可以使用键访问它

res_count = json_response['meta']['result_count']

推荐阅读