[英]Extract single value from JSON data using key
I have the following JSON data:我有以下 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]
]
}
I only want the value for "heat_index".我只想要“heat_index”的值。 In this case that "heat_index" = 33.3.在这种情况下,“heat_index”= 33.3。 I've not been able to figure out how.我一直无法弄清楚如何。 Can anyone give me a hand with this?任何人都可以帮我解决这个问题吗? Thanks for any help provided!感谢您提供的任何帮助!
If you have a json object stored in string format then first you need to decode it before you can access it using the " [ ] " operators.如果您有一个以字符串格式存储的 json 对象,那么首先您需要对其进行解码,然后才能使用“ [] ”运算符访问它。 Luckily Python as usual already has a library for this.幸运的是,像往常一样,Python 已经为此提供了一个库。 The json library has the ability to encode, decode and pretty print json data. 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"])
It doesn't matter too much how you get the json string as long as you call the loads function on it.只要在其上调用loads 函数,如何获取json 字符串并不重要。
Yours Aye,你的 是的,
Omar EQ奥马尔情商
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.