Spotify API:使用jQuery / ajax检索歌曲/艺术​​家/专辑信息

时间:2016-11-01 14:02:15

标签: jquery json ajax spotify

我已经做了一些搜索,我发现了一些类似的东西,但是我仍然在努力解决这个问题。

我尝试发送搜索艺术家的请求并使用jQuery跟踪Spotify API,然后从我收到的json中检索数据。

这是我的代码:

    $.ajax({
        url: "https://api.spotify.com/v1/search?q=" + song + "%20" + artist + "&type=track&limit=1",
        dataType: "json",
        success: function(data) {

            //data downloaded so we call parseJSON function 
            //and pass downloaded data
            var json = $.parseJSON(data);
            //now json variable contains data in json format
            //let's display a few items
            var trackName = json.items.name;
        },

        error: function() {
          console.log("Error retrieving spotify API");
        }
    });

在控制台中,它显示了这个:

XHR finished loading: GET "https://api.spotify.com/v1/search?q=Ride%20Twenty%20One%20Pilots&type=track&limit=1".

这告诉我请求正确发送,json spotify响应是:

{
  "tracks" : {
"href" : "https://api.spotify.com/v1/search?query=Ride+Twenty+One+Pilots&offset=0&limit=1&type=track",
"items" : [ {
  "album" : {
    "album_type" : "album",
    "artists" : [ {
      "external_urls" : {
        "spotify" : "https://open.spotify.com/artist/3YQKmKGau1PzlVlkL1iodx"
      },
      "href" : "https://api.spotify.com/v1/artists/3YQKmKGau1PzlVlkL1iodx",
      "id" : "3YQKmKGau1PzlVlkL1iodx",
      "name" : "Twenty One Pilots",
      "type" : "artist",
      "uri" : "spotify:artist:3YQKmKGau1PzlVlkL1iodx"
    } ],
    "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "US", "UY" ],
    "external_urls" : {
      "spotify" : "https://open.spotify.com/album/3cQO7jp5S9qLBoIVtbkSM1"
    },
    "href" : "https://api.spotify.com/v1/albums/3cQO7jp5S9qLBoIVtbkSM1",
    "id" : "3cQO7jp5S9qLBoIVtbkSM1",
    "images" : [ {
      "height" : 640,
      "url" : "https://i.scdn.co/image/52fc1b3b08807194b87cd7e4fd68f5118d991e44",
      "width" : 640
    }, {
      "height" : 300,
      "url" : "https://i.scdn.co/image/cd5eb6933cca9421578e3badfed816f046f3a86e",
      "width" : 300
    }, {
      "height" : 64,
      "url" : "https://i.scdn.co/image/7f143f49de9521bc762c68cd29ff251f94244c28",
      "width" : 64
    } ],
    "name" : "Blurryface",
    "type" : "album",
    "uri" : "spotify:album:3cQO7jp5S9qLBoIVtbkSM1"
  },
  "artists" : [ {
    "external_urls" : {
      "spotify" : "https://open.spotify.com/artist/3YQKmKGau1PzlVlkL1iodx"
    },
    "href" : "https://api.spotify.com/v1/artists/3YQKmKGau1PzlVlkL1iodx",
    "id" : "3YQKmKGau1PzlVlkL1iodx",
    "name" : "Twenty One Pilots",
    "type" : "artist",
    "uri" : "spotify:artist:3YQKmKGau1PzlVlkL1iodx"
  } ],
  "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "US", "UY" ],
  "disc_number" : 1,
  "duration_ms" : 214506,
  "explicit" : false,
  "external_ids" : {
    "isrc" : "USAT21500598"
  },
  "external_urls" : {
    "spotify" : "https://open.spotify.com/track/2Z8WuEywRWYTKe1NybPQEW"
  },
  "href" : "https://api.spotify.com/v1/tracks/2Z8WuEywRWYTKe1NybPQEW",
  "id" : "2Z8WuEywRWYTKe1NybPQEW",
  "name" : "Ride",
  "popularity" : 91,
  "preview_url" : "https://p.scdn.co/mp3-preview/26fc2318d6ebad09ae7aed7adfce2b28413cea7e",
  "track_number" : 3,
  "type" : "track",
  "uri" : "spotify:track:2Z8WuEywRWYTKe1NybPQEW"
} ],
"limit" : 1,
"next" : "https://api.spotify.com/v1/search?query=Ride+Twenty+One+Pilots&offset=1&limit=1&type=track",
"offset" : 0,
"previous" : null,
"total" : 20
}
}

我试图检索以下信息并将其保存到变量中: 歌曲名称,艺术家,专辑封面&在spotify上链接到专辑。

我尝试过使用

var trackName = json.tracks.items.name;

但控制台出现此错误:

(index):465 Uncaught TypeError: Cannot read property 'name' of undefined

当我查看它的位置时,我试图在代码中设置变量。

如果有人能指出我正确的方向,或者对我有所帮助,我将不胜感激!

谢谢

1 个答案:

答案 0 :(得分:0)

尝试使用可行的“data.tracks.items.name”。