如何正确定位来自spotify API的数据?

时间:2017-04-03 23:47:55

标签: javascript spotify

我正在尝试将艺术家的名字显示在我创建的叠加层上,如何正确定位代码以进行访问?因为,data.artist [0] .name不起作用?

// Get the Album Information from the API to show on the overlay

    function albumInformation(data){

    var infoHTML = '<ul id="data">';

    $.each(data.tracks, function(i, data){
      infoHTML += '<li class="albumInfo">';
      infoHTML += '<p' + data.artist[0].name + '"></p>';
      infoHTML += '</li>';
    }); // closes each

    infoHTML += '</ul>';

  $.getJSON(spotifyURL, albumData, albumInformation);

      $(lightboxOverlay).append(infoHTML);

Spotify API信息

{
  "tracks" : [ {
    "album" : {
      "album_type" : "album",
      "artists" : [ {
        "external_urls" : {
          "spotify" :    
        "https://open.spotify.com/artist/3WrFJ7ztbogyGnTHbHJFl2"
        },
        "href" : "https://api.spotify.com/v1/artists/3WrFJ7ztbogyGnTHbHJFl2",
        "id" : "3WrFJ7ztbogyGnTHbHJFl2",
        "name" : "The Beatles",
        "type" : "artist",
        "uri" : "spotify:artist:3WrFJ7ztbogyGnTHbHJFl2"
      } ],
      "available_markets" : [ "CA", "MX", "US" ],
      "external_urls" : {
        "spotify" : "https://open.spotify.com/album/0ETFjACtuP2ADo6LFhL6HN"
      },
      "href" : "https://api.spotify.com/v1/albums/0ETFjACtuP2ADo6LFhL6HN",
      "id" : "0ETFjACtuP2ADo6LFhL6HN",

1 个答案:

答案 0 :(得分:0)

您缺少数据的album级别。要从有效负载访问艺术家姓名,您需要以下内容:

data.tracks[0].album.artists[0].name;

因此,在$.each代码的上下文中,它将是:

 $.each(data.tracks, function(i, track){
   infoHTML += '<li class="albumInfo">';
   infoHTML += '<p' + track.album.artists[0].name + '"></p>';
   infoHTML += '</li>';
 });