我想從json源獲取溫度。它的嵌套,我無法弄清楚如何從一個JSON文件或URL嵌套值 所以這纔是我到目前爲止的代碼:如何提取嵌套json源中的值?
#! /usr/bin/python
import urllib2
import json
f = urllib2.urlopen('http://api.openweathermap.org/data/2.5/find?q=London&units=metric')
json_string = f.read()
parsed_json = json.loads(json_string)
temp = parsed_json['list']
print "Current temperature is: %s" % (temp)
f.close()
現在我可以一次獲得所有的值,但不只是一個特定的值(在我的情況下,溫度) 我更喜歡在沒有u'temp'的情況下獲取值:如果可能的話。
優秀@ b4hand 這解決了我的情況好和容易。我實際上正在爲此奮鬥幾個小時:-) – ksp 2014-10-08 23:08:00
如果只有某種[客觀的方式來表示讚賞](http://stackoverflow.com/help/accepted-answer)的答案。 – b4hand 2014-11-11 08:47:22