解析从节点服务器 Spotify 返回的 json
Parse json returned from node server Spotify
我想从节点服务器向前端发回格式正确的JSON。
var express = require('express');
var app = express();
app.get('/route', function(req, res){
res.json(tracks)
});
然后在前端,我使用 jQuery
发出 GET
请求
$.get('http://localhost:8888/route', function(data){
console.log(JSON.parse(data));
});
这是返回数据,但是,它是以字符串形式返回的。我尝试使用 JSON.parse
但收到以下错误消息。 data
节点端是一个对象数组。也许这会导致问题。
Uncaught SyntaxError: Unexpected token , in JSON at position 2847(…)
要填充曲目数组,我有以下代码
tracks = [];
for (var i = 0; i < songLinks.length; i++) {
request('https://api.spotify.com/v1/tracks/' + songLinks[i].split(":").pop(), function(error, response, body) {
tracks.push(body)
});
}
曲目数组中的第一项是:
"{↵ "album" : {↵ "album_type" : "album",↵ "artists" : [ {↵
"external_urls" : {↵ "spotify" :
"https://open.spotify.com/artist/1yAwtBaoHLEDWAnWR87hBT"↵ },↵
"href" : "https://api.spotify.com/v1/artists/1yAwtBaoHLEDWAnWR87hBT",↵
"id" : "1yAwtBaoHLEDWAnWR87hBT",↵ "name" : "Modest Mouse",↵
"type" : "artist",↵ "uri" :
"spotify:artist:1yAwtBaoHLEDWAnWR87hBT"↵ } ],↵
"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", "LI", "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/4YvdAtWh6LlnIrv0qIqkCF"↵ },↵
"href" : "https://api.spotify.com/v1/albums/4YvdAtWh6LlnIrv0qIqkCF",↵
"id" : "4YvdAtWh6LlnIrv0qIqkCF",↵ "images" : [ {↵ "height" :
640,↵ "url" :
"https://i.scdn.co/image/411d254a0e46f509dda22f58d699782f16f7bd44",↵
"width" : 640↵ }, {↵ "height" : 300,↵ "url" :
"https://i.scdn.co/image/982b3b7c3ad3e81aed9e0475da07894262f93923",↵
"width" : 300↵ }, {↵ "height" : 64,↵ "url" :
"https://i.scdn.co/image/e4a25681c1163b349ff71b464d2cfc8fda089d58",↵
"width" : 64↵ } ],↵ "name" : "No One's First, And You're Next",↵
"type" : "album",↵ "uri" : "spotify:album:4YvdAtWh6LlnIrv0qIqkCF"↵
},↵ "artists" : [ {↵ "external_urls" : {↵ "spotify" :
"https://open.spotify.com/artist/1yAwtBaoHLEDWAnWR87hBT"↵ },↵
"href" : "https://api.spotify.com/v1/artists/1yAwtBaoHLEDWAnWR87hBT",↵
"id" : "1yAwtBaoHLEDWAnWR87hBT",↵ "name" : "Modest Mouse",↵
"type" : "artist",↵ "uri" :
"spotify:artist:1yAwtBaoHLEDWAnWR87hBT"↵ } ],↵ "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", "LI", "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" :
330573,↵ "explicit" : false,↵ "external_ids" : {↵ "isrc" :
"USSM10700655"↵ },↵ "external_urls" : {↵ "spotify" :
"https://open.spotify.com/track/6Z3pIqEp5n5faSopwto0tB"↵ },↵ "href"
: "https://api.spotify.com/v1/tracks/6Z3pIqEp5n5faSopwto0tB",↵ "id" :
"6Z3pIqEp5n5faSopwto0tB",↵ "name" : "King Rat",↵ "popularity" : 38,↵
"preview_url" :
"https://p.scdn.co/mp3-preview/037294fdd97c94bebf14e7ec1b1fb2c0a1986adc",↵
"track_number" : 7,↵ "type" : "track",↵ "uri" :
"spotify:track:6Z3pIqEp5n5faSopwto0tB"↵}"
下面是回复的屏幕截图,也是为了让我的问题更清楚。
将正文更改为响应
request('https://api.spotify.com/v1/tracks/' + songLinks[i].split(":").pop(), function(error, response, body) {
tracks.push(JSON.parse(body));
});
我想从节点服务器向前端发回格式正确的JSON。
var express = require('express');
var app = express();
app.get('/route', function(req, res){
res.json(tracks)
});
然后在前端,我使用 jQuery
发出GET
请求
$.get('http://localhost:8888/route', function(data){
console.log(JSON.parse(data));
});
这是返回数据,但是,它是以字符串形式返回的。我尝试使用 JSON.parse
但收到以下错误消息。 data
节点端是一个对象数组。也许这会导致问题。
Uncaught SyntaxError: Unexpected token , in JSON at position 2847(…)
要填充曲目数组,我有以下代码
tracks = [];
for (var i = 0; i < songLinks.length; i++) {
request('https://api.spotify.com/v1/tracks/' + songLinks[i].split(":").pop(), function(error, response, body) {
tracks.push(body)
});
}
曲目数组中的第一项是:
"{↵ "album" : {↵ "album_type" : "album",↵ "artists" : [ {↵
"external_urls" : {↵ "spotify" : "https://open.spotify.com/artist/1yAwtBaoHLEDWAnWR87hBT"↵ },↵
"href" : "https://api.spotify.com/v1/artists/1yAwtBaoHLEDWAnWR87hBT",↵ "id" : "1yAwtBaoHLEDWAnWR87hBT",↵ "name" : "Modest Mouse",↵
"type" : "artist",↵ "uri" : "spotify:artist:1yAwtBaoHLEDWAnWR87hBT"↵ } ],↵
"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", "LI", "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/4YvdAtWh6LlnIrv0qIqkCF"↵ },↵
"href" : "https://api.spotify.com/v1/albums/4YvdAtWh6LlnIrv0qIqkCF",↵ "id" : "4YvdAtWh6LlnIrv0qIqkCF",↵ "images" : [ {↵ "height" : 640,↵ "url" : "https://i.scdn.co/image/411d254a0e46f509dda22f58d699782f16f7bd44",↵
"width" : 640↵ }, {↵ "height" : 300,↵ "url" : "https://i.scdn.co/image/982b3b7c3ad3e81aed9e0475da07894262f93923",↵
"width" : 300↵ }, {↵ "height" : 64,↵ "url" : "https://i.scdn.co/image/e4a25681c1163b349ff71b464d2cfc8fda089d58",↵
"width" : 64↵ } ],↵ "name" : "No One's First, And You're Next",↵ "type" : "album",↵ "uri" : "spotify:album:4YvdAtWh6LlnIrv0qIqkCF"↵ },↵ "artists" : [ {↵ "external_urls" : {↵ "spotify" : "https://open.spotify.com/artist/1yAwtBaoHLEDWAnWR87hBT"↵ },↵
"href" : "https://api.spotify.com/v1/artists/1yAwtBaoHLEDWAnWR87hBT",↵ "id" : "1yAwtBaoHLEDWAnWR87hBT",↵ "name" : "Modest Mouse",↵
"type" : "artist",↵ "uri" : "spotify:artist:1yAwtBaoHLEDWAnWR87hBT"↵ } ],↵ "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", "LI", "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" : 330573,↵ "explicit" : false,↵ "external_ids" : {↵ "isrc" : "USSM10700655"↵ },↵ "external_urls" : {↵ "spotify" : "https://open.spotify.com/track/6Z3pIqEp5n5faSopwto0tB"↵ },↵ "href" : "https://api.spotify.com/v1/tracks/6Z3pIqEp5n5faSopwto0tB",↵ "id" : "6Z3pIqEp5n5faSopwto0tB",↵ "name" : "King Rat",↵ "popularity" : 38,↵ "preview_url" : "https://p.scdn.co/mp3-preview/037294fdd97c94bebf14e7ec1b1fb2c0a1986adc",↵ "track_number" : 7,↵ "type" : "track",↵ "uri" : "spotify:track:6Z3pIqEp5n5faSopwto0tB"↵}"
下面是回复的屏幕截图,也是为了让我的问题更清楚。
将正文更改为响应
request('https://api.spotify.com/v1/tracks/' + songLinks[i].split(":").pop(), function(error, response, body) {
tracks.push(JSON.parse(body));
});