I received a list when inputting the following URL - http://api.twitter.com/1/trends/44418.json
The list contains multiple dictionaries, and I'm a bit confused with the list structure. I'm trying to obtain the values associated with the 'name' key.
For example:
"name":"#throwagrenade"
"name":"Rebecca Black"
"name":"#questionsihate"
I can write the code myself, I'm just trying to conceptually understand how to access dictionaries (and their key/value pairs) within a list.
解决方案
The first thing I would do when working with a big lump of json, is try to get it into a more readable format. This online json formatting tool should do the job.
Here's some code that will get all the trend names:
import urllib2
import json
url = 'http://api.twitter.com/1/trends/44418.json'
# download the json string
json_string = urllib2.urlopen(url).read()
# de-serialize the string so that we can work with it
the_data = json.loads(json_string)
# get the list of trends
trends = the_data[0]['trends']
# print the name of each trend
for trend in trends:
print trend['name']
Or you can do it all in one line:
names = [trend['name'] for trend in the_data[0]['trends']]
for name in names:
print name
Both will result in:
#throwagrenade
Rebecca Black
Eric Abidal
#questionsihate
#juniordoctors
Smiley Culture
Lily Allen
Wes Brown
Pandev
Ray Wilkins
Relevant reading:
Python docs on json (although you should only really need json.loads())