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

我已经完成了一些搜索,并且发现了一些与之相似的东西,但是我仍然在努力探索它的工作方式,并且努力了一些。

我试图发送请求来搜索艺术家,并使用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

当我看看这是什么时,它就是我试图在代码中设置变量的地方。

如果任何人都能指引我朝着正确的方向发展,或者对我有所帮助,那将非常感谢!

谢谢


尝试使用“data.tracks.items.name”,将工作。

链接地址: http://www.djcxy.com/p/55577.html

上一篇: Spotify API: Retrieving song/artist/album information using jQuery/ajax

下一篇: Unable to iterate over (actual) results in Spotify API search