python - 使用 Python 从 json 文件中读取特定值
问题描述
我从 curl 请求中获取 JSON 文件,我想从中读取特定值。假设我有一个 JSON 文件,如下所示。如何将“result_count”值插入变量?
目前,在得到 curl 的响应后,我正在将 JSON 对象写入这样的 txt 文件中。
json_response = connect_to_endpoint(url, headers)
f.write(json.dumps(json_response, indent=4, sort_keys=True))
解决方案
您json_response
不是 JSON 内容(JSON 是格式化字符串),而是 a python dict
,您可以使用键访问它
res_count = json_response['meta']['result_count']
推荐阅读
- c# - SQL 如何替换一个值
- python - 使用 python 和请求,如何进行正确的 POST 调用以及在哪里找到标头?
- android - FirebaseAppDistribution:appDistributionUpload gradle 命令中缺少应用程序 ID
- mysql - 跨列和行计算值的实例
- javascript - 使用 redux 钩子时使用 redux 操作的最佳选择是什么?
- sql - Impala 2.11:AnalysisException:选择列表中不支持子查询
- swift - 在 SwiftUI 中获取当前的经纬度
- c++ - 是否可以禁止一个类被动态转换为?
- git - Git - 将发布分支合并到主分支
- tensorflow - 任何想法如何解决激活函数的问题?