如何将 JSON 结果放入 Python 变量?
How to get a JSON result into a Python variable?
我想通过 Yahoo! 获取温度温度 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'}}}}
我需要的信息是来自u'temp': u'+6'
的+6
。
如何将它引用到变量中?
>>> 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'
我想通过 Yahoo! 获取温度温度 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'}}}}
我需要的信息是来自u'temp': u'+6'
的+6
。
如何将它引用到变量中?
>>> 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'