【发布时间】:2019-11-19 17:25:42
【问题描述】:
我试图弄清楚如何解析我从 Spotify API 获得的 JSON 数据。我正在使用这个节点模块https://www.npmjs.com/package/spotify-web-api-js 来获取 Spotify 播放列表曲目。
我正在使用它来获取我的 json(看看我在那里做了什么)
export class HomePage {
spotifyApi = new SpotifyWebApi;
constructor() {}
}
var spotifyApi = new SpotifyWebApi();
spotifyApi.setAccessToken('Spotify OAuth Token');
spotifyApi.getPlaylistTracks('37i9dQZEVXbMDoHDwVN2tF')
.then(function(data) {
console.log('Playlist Tracks', data);
}, function(err) {
console.error(err);
var prev = null;
function onUserInput(queryTerm) {
// abort previous request, if any
if (prev !== null) {
prev.abort();
}
// store the current promise in case we need to abort it
prev = spotifyApi.searchTracks(queryTerm, {limit: 5});
prev.then(function(data) {
// clean the promise so it doesn't call abort
prev = null;
// ...render list of search results...
}, function(err) {
console.error(err);
});
}
这会返回一个 JSON 文件,但由于某种原因(可能是我的错误),当我使用 JSON.parse(data);
console.log(data.name) 时它不起作用(我知道我在这里做错了,但我不知道如何修复它)。提前致谢:{)
【问题讨论】: