Access Item In Json Result
I am a bit confused as to how access artists name in this JSON result: { 'tracks' : { 'href' : 'https://api.spotify.com/v1/search?query=karma+police&offset=0&limit=20
Solution 1:
artists
is a list of dicts, because a track can have many artists. Similarly items
is also a list. So for example you can do results['tracks']['items'][0]['artists'][0]['name']
, but that will only get you the first artist of the first track.
Solution 2:
Defensively written, this might look something like:
artist_names = set() # using a set avoids adding the same name more than onceif'tracks'in results and 'items'in results['tracks']:
for item in results['tracks']['items']:
for artist in item.get('artists', ()):
if'name'in artist:
artist_names.add(artist['name'])
for name in artist_names:
print"Found artist:", name
Post a Comment for "Access Item In Json Result"