python - 使用键从 JSON 数据中提取单个值
问题描述
我有以下 JSON 数据:
{
"status": {
"status_code": 0,
"status_message": "SUCCESS"
},
"device_id": 89911454,
"type": "obs_st",
"source": "cache",
"summary": {
"pressure_trend": "falling",
"strike_count_1h": 0,
"strike_count_3h": 0,
"precip_total_1h": 0.0,
"precip_accum_local_yesterday": 0.0,
"precip_accum_local_yesterday_final": 0.0,
"precip_analysis_type_yesterday": 1,
"feels_like": 33.3,
"heat_index": 33.3,
"wind_chill": 33.3
},
"obs": [
[1600214324, 0, 0, 0, 0, 3, 1012.8, 33.3, 35, 27898, 1.94, 232, 0, 0, 0, 0, 2.56, 1, 0, null, null, 0]
]
}
我只想要“heat_index”的值。在这种情况下,“heat_index”= 33.3。我一直无法弄清楚如何。任何人都可以帮我解决这个问题吗?感谢您提供的任何帮助!
解决方案
如果您有一个以字符串格式存储的 json 对象,那么首先您需要对其进行解码,然后才能使用“ [ ] ”运算符访问它。幸运的是,Python 像往常一样已经为此提供了一个库。json库具有编码、解码和漂亮打印 json 数据的能力。
import json
file = open("your-json-file-here.json")
jsonString = file.read()
file.close()
jsonObject = json.loads(jsonString)
print(jsonObject["summary"]["heat_index"])
只要您在其上调用loads函数,如何获取json字符串并不重要。
你的 是的,
奥马尔情商
推荐阅读
- android - firebase_database 中 onChildAdded 流的意外行为
- azure-devops - Azure Devops 中是否存在 $(SourceVersion) 的 7 位短版本?
- java - Timescaledb - 通过 Java/JDBC 创建超表
- reactjs - 如何在本机反应中循环多个数组
- python - 如何修复 AttributeError:'_Screen' 对象在 python 龟中没有属性 'onkeypress'?
- c++ - grep 文本文件中的字符串并将它们复制到 CMake 中的目录
- android - Android:如何将推送通知集成到现有应用程序中?
- sql-server - 匹配时合并 - 使用查询结果更新集
- logging - 我可以使用 ILogger
与企业图书馆日志记录? - spring - 如何在字段验证器中传递其他参数?