[英]nodejs parse object in json response
I decided to try something new this weekend and started a NodeJS project, which should give me a random new Spotify song.我决定在这个周末尝试一些新的东西,并开始了一个 NodeJS 项目,它应该会给我一首随机的新 Spotify 歌曲。 Therefore I'm using the
node-spotify-api
npm module.因此我使用
node-spotify-api
npm 模块。 It seems like i managed to authenticate to the Spotify API but I don't know how to access the response correctly.似乎我设法对 Spotify API 进行了身份验证,但我不知道如何正确访问响应。 Here is my code so far:
到目前为止,这是我的代码:
// Get random Song
app.get('/getrandomsong', async (req, res) => {
console.log('Starting to search for a random song');
// Authenticate to spotify api
const spotify = new Spotify({
id: process.env.SPOTIFY_ID,
secret: process.env.SPOTIFY_SECRET
});
// Make the actual request
spotify
.request('https://api.spotify.com/v1/browse/new-releases?limit=1')
.then(function(data) {
console.log(data);
}) .catch(function(err) {
console.log('Error occured: ' + err);
});
});
The console log shows me the following response.控制台日志显示了以下响应。 How can I access and use the information in
[Object]
?如何访问和使用
[Object]
的信息?
Starting to search for a random song
{
albums: {
href: 'https://api.spotify.com/v1/browse/new-releases?offset=0&limit=1',
items: [ [Object] ],
limit: 1,
next: 'https://api.spotify.com/v1/browse/new-releases?offset=1&limit=1',
offset: 0,
previous: null,
total: 100
}
}
You can access it by using data.albums.items
您可以使用
data.albums.items
访问它
You can also check the content of the items array to see what your Object is, by doing the following您还可以通过执行以下操作来检查 items 数组的内容以查看您的对象是什么
data.albums.items.forEach(arr => {
console.log(arr)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.