json_data=open('...\spins.json')
data = json.load(json_data)
pprint(data)
json_data.close()
显示出以下内容:
[{u'endTime': u'1317752614105',
u'id': u'cf37894e8eaf886a0d000000',
u'length': 492330,
u'metadata': {u'album': u'Mezzanine',
u'artist': u'Massive Attack',
u'bitrate': 128000,
u'label': u'Virgin',
u'length': 17494.479054779807,
u'title': u'Group Four'},
这个只是显示的一部分内容
1、我想从一个网址(顶部提供的网址)中抓取它。2、仅获取“专辑”,“艺术家”和“标题” 3、确保它尽可能显示简单:Artist
Track title
Album
Artist
Track title
Album
4、希望我能得到一些帮助,我真的很想为自己创建一个,这样我可以检查更多的音乐! 来自马文!
Python (after you loaded the json)
for elem in data:
print('{artist}
{title}
{album}
'.format(**elem['metadata']))
To save in a file:
with open('the_file_name.txt','w') as f:
for elem in data:
f.write('{artist}
{title}
{album}
'.format(**elem['metadata']))
Okay this is a bit short but the thing about json is that it translate an array into a string
eg. array['first'] = 'hello'; array['second'] = 'there';
will become [{u'first': u'hello', u'second': 'there'}]; after a jsonencode run that sting throu jsondecode and you get your array back
so simply run you json file thou a decoder and then you should be able to reach your data through:
array['metadata'].album
array['metadata'].artist
...
have never used python but it should be the same.
have a look at http://www.php.net/manual/en/function.json-decode.php it might clear upp a thing or two.
For PHP you need json.decode
<?php
$json = file_get_contents($url);
$val = json_decode($json);
$room = $val[0]->metadata;
echo "Album : ".$room->album."
";
echo "Artist : ".$room->artist."
";
echo "Title : ".$room->title."
";
?>
Outputs
Album : Future Sandwich
Artist : Them, Roaringtwenties
Title : Fast Acting Nite-Nite Spray With Realistic Uncle Beard
Note its a truck load of JSON data there so you'll have to iterate adequately
You're already really close.
data = json.load(json_data)
is taking the JSON string and converting it to a Python object - in this case, a list of dictionaries (plus 'metadata', which is a dictionary of dictionaries).
To get this into the format that you want, you just need to loop through the items.
for song in data:
artist = song['metadata']['artist'] # This tells it where to look in the dictionary. It's looking for the dictionary item called 'metadata'. Then, looking inside that dictionary for 'artist'.
album = song['metadata'['album']
songTitle = song['metadata']['title']
print '%s
%s
%s
' % (artist, album, songTitle)
Or, to print it to a file:
with open('the_file_name.txt','w') as f:
for song in data:
artist = song['metadata']['artist']
album = song['metadata'['album']
songTitle = song['metadata']['title']
f.write('%s
%s
%s
' % (artist, album, songTitle))