这里是异步编程的新手。
我正在尝试获取JSON数据并在节点中解析它。但出于某种原因,我不断收到错误:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: JSON.parse is not a function
fetch(url).then(function(res) {
return res.json();
}).then(function(json) {
jsonObj=JSON.parse(json);
console.log(json);
console.log("HELLO");
console.log(jsonObj.id);
});
jsonObj=JSON.parse(json);
被注释掉时的JSON数据:
[ { id: 1,
url: 'http://www.tvmaze.com/shows/1/under-the-dome',
name: 'Under the Dome',
type: 'Scripted',
language: 'English',
genres: [ 'Drama', 'Science-Fiction', 'Thriller' ],
status: 'Ended',
runtime: 60,
premiered: '2013-06-24',
schedule: { time: '22:00', days: [Object] },
rating: { average: 6.6 },
weight: 0,
network: { id: 2, name: 'CBS', country: [Object] },
webChannel: null,
externals: { tvrage: 25988, thetvdb: 264492, imdb: 'tt1553656' },
image:
{ medium: 'http://static.tvmaze.com/uploads/images/medium_portrait/0/1.jpg',
original: 'http://static.tvmaze.com/uploads/images/original_untouched/0/1.jpg' },
summary: '<p>Inhabitants must deal with surviving the post-apocalyptic conditions</p>',
updated: 1490211618,
_links: { self: [Object], previousepisode: [Object] } }, ... 140 more items
答案 0 :(得分:0)
您的错误表明您已重新定义了JSON对象本身 - 就像这样;
$ node
> a='{"a":7}'
'{"a":7}'
> JSON.parse(a)
{ a: 7 }
> JSON=7
7
> JSON.parse(a)
TypeError: JSON.parse is not a function
检查JSON和JSON.parse的值是什么,我打赌它不是函数