使用Songkick API时,JSON.parse()因意外标记而失败
我正在尝试使用以下节点解析来自Songkick API的响应:使用Songkick API时,JSON.parse()因意外标记而失败,json,node.js,api,Json,Node.js,Api,我正在尝试使用以下节点解析来自Songkick API的响应: router.post('/', function(req, res) { var artist = req.body.artist; var id; res.render('artist', { title: 'Artist', artist: artist }); http.request({host: 'api.songkick.com', path: '/api/3.0/search/artists.json
router.post('/', function(req, res) {
var artist = req.body.artist;
var id;
res.render('artist', { title: 'Artist', artist: artist });
http.request({host: 'api.songkick.com', path: '/api/3.0/search/artists.json?query=' + artist + '&apikey=myAPIKey'}, function(res) {
res.on("data", function(chunk) {
id = JSON.parse(chunk).resultsPage.results.artist[0].id;
console.log(id);
http.request({host: 'api.songkick.com', path: '/api/3.0/artists/' + id + '/gigography.json?apikey=Z2JWQTvgk4tsCdDn'}, function(res) {
res.on("data", function(chunk) {
console.log(JSON.parse(chunk).resultsPage.results);
});
}).end();
});
}).end();
});
但是,JSON.parse失败,出现SyntaxError:Unexpected token t
(最后一个字符根据请求的艺术家而变化)
如何修复语法错误?我会尝试压缩字符串(以块的形式到达),然后使用结束事件。比如:
router.post('/', function(req, res) {
var artist = req.body.artist;
var id;
var data1 = '';
res.render('artist', { title: 'Artist', artist: artist });
http.request({host: 'api.songkick.com', path: '/api/3.0/search/artists.json?query=' +
artist + '&apikey=myAPIKey'}, function(res) {
res.on("data", function(chunk) {
data1 += chunk;
});
res.on('end', function(){
id = JSON.parse(data1).resultsPage.results.artist[0].id;
console.log(id);
var data2 = '';
http.request({host: 'api.songkick.com', path: '/api/3.0/artists/' + id + '/gigography.json?apikey=Z2JWQTvgk4tsCdDn'}, function(res) {
res.on("data", function(chunk) {
data2 += chunk;
});
res.on('end', function(){
console.log(JSON.parse(data2).resultsPage.results);
});
}).end();
});
}).end();
});
基本上,它使用data1和data2作为缓冲区,仅当所有数据到达时才调用函数。基本上与另一个答案相同。您需要连接数据块,注册一个结束事件,并在那里进行解析。 这是我试过的一个,它似乎像预期的那样工作
var http = require('http');
var artist = 'Beatles'
http.request({host: 'api.songkick.com', path: '/api/3.0/search/artists.json?query=' + artist + '&apikey=Z2JWQTvgk4tsCdDn'}, function(res) {
var response_data = '';
var id;
res.on("data", function(chunk) {
response_data += chunk;
});
res.on('end', function(){
id = JSON.parse(response_data).resultsPage.results.artist[0].id;
console.log(id);
http.request({host: 'api.songkick.com', path: '/api/3.0/artists/' + id + '/gigography.json?apikey=Z2JWQTvgk4tsCdDn'}, function(res) {
var inner_data = '';
res.on("data", function(chunk) {
inner_data += chunk;
});
res.on("end", function(){
console.log(JSON.parse(inner_data).resultsPage.results);
});
}).end();
});
}).end();
解析(块)你是对的。。。如果我记录chunk,它会给我一系列缓冲区。我怎样才能从这些转换到可解析的JSON呢?我对你的代码做了一些修改,并给出了答案。但是,它没有在实际操作中进行测试。Hmm提供了
JSON.parse(data1.resultsPage.results.artist[0].id;^TypeError:无法读取未定义的属性“artist”
嗯,我不知道结果字符串的json。您提供的API示例仅在结果下面列出了一个“事件”属性,而没有“艺术家”的名称。使用类似的方法来可视化您得到的json响应。啊,顺便说一句:如果[0]是一个数组,那么你只能使用它。json中的数组类似于[value1,value2,value2](注意不同的大括号)。