【问题标题】:Spotify API: Retrieving song/artist/album information using jQuery/ajaxSpotify API:使用 jQuery/ajax 检索歌曲/艺术​​家/专辑信息
【发布时间】:2016-11-01 14:02:15
【问题描述】:

我进行了一些搜索,发现了一些有点相似的东西,但我仍然有点难以理解它的工作方式。

我正在尝试使用 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

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

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

谢谢

【问题讨论】:

  • items 是一个数组,因此您需要通过索引访问它。以json.tracks.items[0].name 为例
  • 我现在收到此错误:'Uncaught TypeError: Cannot read property 'tracks' of null'
  • 那么你的json变量是null
  • 感谢您的快速回复,我现在得到了一些东西。我在索引错误的东西

标签: jquery json ajax spotify


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-05-22
    • 2015-01-30
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    相关资源
    最近更新 更多