Spotify json响应是否包含换行符?

时间:2015-10-22 20:10:15

标签: javascript json http meteor spotify

我正在使用Meteors http使用Spotify API搜索曲目。我的问题是,当服务器获取Json文件时,文件中有新的行字符,所以我无法解析它。我找到了一个solution来逃避新行字符,但我怀疑必须有更好的方法。

我的HTTP电话

function artFromSpotify(title, artist, album){
  var qString = 'artist:' + artist + ' track:' + title;
  var result = HTTP.call("GET", 'https://api.spotify.com/v1/search?'+qString, {
    params: {'q': qString,
      'type' : 'track',
      'limit' :'1'}
    });   

Spotify JSON响应

content: '{\n  "tracks" : {\n    "href" : "https://api.spotify.com/v1/search?query=artist%3AElton+John+track%3ARocket+Man&offset=0&limit=1&type=track",\n    "items" : [ {\n      "album" : {\n        "album_type" : "album",\n        "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "IE", "IS", "IT", "LI", "LT", "LU", "LV", "MC", "MT", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "RO", "SE", "SG", "SI", "SK", "SV", "TR", "TW", "UY" ],\n        "external_urls" : {\n          "spotify" : "https://open.spotify.com/album/46g6b33tbttcPtzbwzBoG6"\n        },\n        "href" : "https://api.spotify.com/v1/albums/46g6b33tbttcPtzbwzBoG6",\n        "id" : "46g6b33tbttcPtzbwzBoG6",\n        "images" : [ {\n          "height" : 634,\n          "url" : "https://i.scdn.co/image/07039e8a1fcd1fda967a021e8c4d5ccaf5dbaa8d",\n          "width" : 640\n        }, {\n          "height" : 297,\n          "url" : "https://i.scdn.co/image/e62555a039122c4c26ea7fb738e705f1de1795d2",\n          "width" : 300\n        }, {\n          "height" : 63,\n          "url" : "https://i.scdn.co/image/c3581519b056a17b9056fa579536e9788868464d",\n          "width" : 64\n        } ],\n        "name" : "Honky Chateau (Remastered)",\n        "type" : "album",\n        "uri" : "spotify:album:46g6b33tbttcPtzbwzBoG6"\n      },\n      "artists" : [ {\n        "external_urls" : {\n          "spotify" : "https://open.spotify.com/artist/3PhoLpVuITZKcymswpck5b"\n        },\n        "href" : "https://api.spotify.com/v1/artists/3PhoLpVuITZKcymswpck5b",\n        "id" : "3PhoLpVuITZKcymswpck5b",\n        "name" : "Elton John",\n        "type" : "artist",\n        "uri" : "spotify:artist:3PhoLpVuITZKcymswpck5b"\n      } ],\n      "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "IE", "IS", "IT", "LI", "LT", "LU", "LV", "MC", "MT", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "RO", "SE", "SG", "SI", "SK", "SV", "TR", "TW", "UY" ],\n      "disc_number" : 1,\n      "duration_ms" : 281613,\n      "explicit" : false,\n      "external_ids" : {\n        "isrc" : "GBAMB7200006"\n      },\n      "external_urls" : {\n        "spotify" : "https://open.spotify.com/track/2zvot9pY2FNl1E94kc4K8M"\n      },\n      "href" : "https://api.spotify.com/v1/tracks/2zvot9pY2FNl1E94kc4K8M",\n      "id" : "2zvot9pY2FNl1E94kc4K8M",\n      "name" : "Rocket Man (I Think It\'s Going To Be A Long Long Time)",\n      "popularity" : 65,\n      "preview_url" : "https://p.scdn.co/mp3-preview/e0f814581fb0c9f5faeb02146aa4c1280510b4f3",\n      "track_number" : 5,\n      "type" : "track",\n      "uri" : "spotify:track:2zvot9pY2FNl1E94kc4K8M"\n    } ],\n    "limit" : 1,\n    "next" : "https://api.spotify.com/v1/search?query=artist%3AElton+John+track%3ARocket+Man&offset=1&limit=1&type=track",\n    "offset" : 0,\n    "previous" : null,\n    "total" : 54\n  }\n}'

1 个答案:

答案 0 :(得分:2)

query = 'https://api.spotify.com/v1/search?query=artist%3AElton+John+track%3ARocket+Man&offset=0&limit=1&type=track'

HTTP.get(query, function(err, res){
  result = JSON.stringify(
    JSON.parse(res.content).tracks
  )
})

result现在将包含没有换行符的json字符串。

但是,对于大多数用途,您实际上需要JSON.parse(res.content)的结果。

HTTP.get(query, function(err, res){
  result = JSON.parse(res.content).tracks
})

现在result是您可以使用的对象。