我有以下 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"])
如何获取json字符串并不重要,只要调用它的loads函数即可。