如何将JSON结果转换为Python变量?

时间:2016-01-17 06:38:04

标签: python json yahoo-api

我想通过雅虎获得温度!将温度API转换为Python变量。

这是我的代码:

import urllib2, urllib, json
baseurl = "https://query.yahooapis.com/v1/public/yql?"
yql_query = "select item.condition from weather.forecast where woeid = 35252 and u='c'"
yql_url = baseurl + urllib.urlencode({'q':yql_query}) + "&format=json"
result = urllib2.urlopen(yql_url).read()
data = json.loads(result)
print data['query']['results']

结果如下:

{u'channel': {u'item': {u'condition': {u'date': u'Sun, 17 Jan 2016 3:50 am CET', u'text': u'Light Snow', u'code': u'14', u'temp': u'+6'}}}}

我需要的信息是来自+6的{​​{1}}。

如何将其引用到变量?

1 个答案:

答案 0 :(得分:3)

>>> d = {u'channel': {u'item': {u'condition': {u'date': u'Sun, 17 Jan 2016 3:50 am CET', u'text': u'Light Snow', u'code': u'14', u'temp': u'+6'}}}}
>>> temp = d['channel']['item']['condition']['temp']
>>> print temp
u'+6'
相关问题