python如何获取嵌套json想要的属性值?

{"data":{"yesterday":{"date":"11日星期二","high":"高温 33℃","fx":"西南风","low":"低温 …
关注者
9
被浏览
16,624

4 个回答

使用标准库json模块,转换成字典。嵌套的内容也会自动转换的,这样就转换成了python数据结构。嵌套的内容多加几个[]就行了,想要什么取就得了。

这不是标准的json吗,python中直接解析成字典就行了啊

import json

s = '{"data":{"yesterday":{"date":"11日星期二","high":"高温 33℃","fx":"西南风","low":"低温 26℃","fl":"微风","type":"阵雨"},"city":"杭州","aqi":"56","forecast":[{"date":"12日星期三","high":"高温 36℃","fengli":"微风级","low":"低温 28℃","fengxiang":"西南风","type":"多云"},{"date":"13日星期四","high":"高温 37℃","fengli":"微风级","low":"低温 28℃","fengxiang":"西南风","type":"多云"},{"date":"14日星期五","high":"高温 36℃","fengli":"微风级","low":"低温 28℃","fengxiang":"南风","type":"多云"},{"date":"15日星期六","high":"高温 36℃","fengli":"微风级","low":"低温 27℃","fengxiang":"南风","type":"多云"},{"date":"16日星期天","high":"高温 36℃","fengli":"微风级","low":"低温 27℃","fengxiang":"东南风","type":"晴"}],"ganmao":"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。","wendu":"31"},"status":1000,"desc":"OK"}'

t = json.loads(s)

print(t['data']['yesterday']['high'])