我是JavaScript和jQuery的新手,但我在学习的同时正在学习。 现在我正在使用JS(https://github.com/fxb/javascript-last.fm-api)的last.fm api,我正在尝试获取列表的第一张专辑。
我使用以下代码获取数据:
lastfm.track.getInfo({track: activeMedia.title, artist: activeMedia.author}, {success: function(trackInfoResult){
// doing things with trackInfoResult
}, error: function(code, message){
console.log("Error code: " + code);
}});
此输出采用以下结构(在http://www.last.fm/api/show/track.getInfo上找到):
<track>
<id>1019817</id>
<name>Believe</name>
<mbid/>
<url>http://www.last.fm/music/Cher/_/Believe</url>
<duration>240000</duration>
<streamable fulltrack="1">1</streamable>
<listeners>69572</listeners>
<playcount>281445</playcount>
<artist>
<name>Cher</name>
<mbid>bfcc6d75-a6a5-4bc6-8282-47aec8531818</mbid>
<url>http://www.last.fm/music/Cher</url>
</artist>
<album position="1">
<artist>Cher</artist>
<title>Believe</title>
<mbid>61bf0388-b8a9-48f4-81d1-7eb02706dfb0</mbid>
<url>http://www.last.fm/music/Cher/Believe</url>
<image size="small">http://userserve-ak.last.fm/serve/34/8674593.jpg</image>
<image size="medium">http://userserve-ak.last.fm/serve/64/8674593.jpg</image>
<image size="large">http://userserve-ak.last.fm/serve/126/8674593.jpg</image>
</album>
<toptags>
<tag>
<name>pop</name>
<url>http://www.last.fm/tag/pop</url>
</tag>
...
</toptags>
<wiki>
<published>Sun, 27 Jul 2008 15:44:58 +0000</published>
<summary>...</summary>
<content>...</content>
</wiki>
</track>
获取我的艺术家姓名和ID我可以通过以下代码管理:
console.log(rackInfoResult.track.name);
但现在我想要第一张专辑。如何选择位置=“1”的相册?
答案 0 :(得分:1)
好的,我找到了。
我使用以下代码来追踪正确的元素
for(var key in trackInfoResult.track) {
var value = trackInfoResult.track[key];
console.log(key + " : " + value);
}
每次添加trackInfoResult.track后面的一个键
答案 1 :(得分:0)
然后你应该尝试这样做。它对我有用。
脚本的成功功能
success: function(trackInfoResult){
var x = eval('('+trackInfoResult+')');
console.log(x);
console.log(x[0]);
}
我认为这对你也有用。
答案 2 :(得分:0)
你可以尝试:
var myAlbum = trackInfoResult.track.album;
if (parseInt(myAlbum.getAttribute('position'), 10) === 1) {
// do stuff here
}