SyntaxError:JSON.parse 处的 JSON 输入意外结束 [重复]
Posted
技术标签:
【中文标题】SyntaxError:JSON.parse 处的 JSON 输入意外结束 [重复]【英文标题】:SyntaxError: Unexpected end of JSON input at JSON.parse [duplicate] 【发布时间】:2022-01-22 09:53:59 【问题描述】:我正在为 Angela Yu 在 Udemy 上的代码训练营开发天气应用程序。一切正常,直到我到达 JSON.parse 行。我不知道为什么会出错。
const https = require("https");
const app = express();
const url = "https://api.openweathermap.org/data/2.5/forecast?appid=API-KEY-OMITED&units=imperial&zip=97078";
const port = 3000;
app.get('/', (req, res) =>
https.get(url, (response) =>
console.log(response.statusCode);
response.on("data", (data) =>
const weatherData = JSON.parse(data)
console.log(weatherData);
)
)
res.send('server is running!')
)
app.listen(port, () =>
console.log(`Server is running on port $port!`);
)
这是我在终端中得到的。
[nodemon] starting `node app.js`
Server is running on port 3000!
200
undefined:1
"cod":"200","message":0,"cnt":40,"list":["dt":1640034000,"main":"temp":40.91,"feels_like":40.91,"temp_min":40.73,"temp_max":40.91,"pressure":1015,"sea_level":1015,"grnd_level":1009,"humidity":92,"temp_kf":0.1,"weather":["id":501,"main":"Rain","description":"moderate rain","icon":"10d"],"clouds":"all":90,"wind":"speed":2.46,"deg":64,"gust":6.2,"visibility":10000,"pop":1,"rain":"3h":5.98,"sys":"pod":"d","dt_txt":"2021-12-20 21:00:00","dt":1640044800,"main":"temp":40.86,"feels_like":40.86,"temp_min":40.77,"temp_max":40.86,"pressure":1016,"sea_level":1016,"grnd_level":1010,"humidity":94,"temp_kf":0.05,"weather":["id":501,"main":"Rain","description":"moderate rain","icon":"10d"],"clouds":"all":93,"wind":"speed":2.71,"deg":31,"gust":4.74,"visibility":10000,"pop":1,"rain":"3h":3.15,"sys":"pod":"d","dt_txt":"2021-12-21 00:00:00","dt":1640055600,"main":"temp":41.09,"feels_like":41.09,"temp_min":41.09,"temp_max":41.18,"pressure":1018,"sea_level":1018,"grnd_level":1012,"humidity":95,"temp_kf":-0.05,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10n"],"clouds":"all":97,"wind":"speed":2.13,"deg":23,"gust":4.07,"visibility":10000,"pop":1,"rain":"3h":1.29,"sys":"pod":"n","dt_txt":"2021-12-21 03:00:00","dt":1640066400,"main":"temp":41.72,"feels_like":41.72,"temp_min":41.72,"temp_max":41.72,"pressure":1020,"sea_level":1020,"grnd_level":1013,"humidity":94,"temp_kf":0,"weather":["id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"],"clouds":"all":100,"wind":"speed":2.55,"deg":51,"gust":2.51,"visibility":10000,"pop":0.87,"sys":"pod":"n","dt_txt":"2021-12-21 06:00:00","dt":1640077200,"main":"temp":41.25,"feels_like":39.47,"temp_min":41.25,"temp_max":41.25,"pressure":1019,"sea_level":1019,"grnd_level":1012,"humidity":93,"temp_kf":0,"weather":["id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"],"clouds":"all":85,"wind":"speed":3.29,"deg":23,"gust":4.05,"visibility":10000,"pop":0,"sys":"pod":"n","dt_txt":"2021-12-21 09:00:00","dt":1640088000,"main":"temp":40.26,"feels_like":37.96,"temp_min":40.26,"temp_max":40.26,"pressure":1019,"sea_level":1019,"grnd_level":1012,"humidity":95,"temp_kf":0,"weather":["id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"],"clouds":"all":89,"wind":"speed":3.65,"deg":21,"gust":5.14,"visibility":10000,"pop":0,"sys":"pod":"n","dt_txt":"2021-12-21 12:00:00","dt":1640098800,"main":"temp":39.15,"feels_like":39.15,"temp_min":39.15,"temp_max":39.15,"pressure":1019,"sea_level":1019,"grnd_level":1012,"humidity":94,"temp_kf":0,"weather":["id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"],"clouds":"all":100,"wind":"speed":1.83,"deg":47,"gust":1.83,"visibility":10000,"pop":0,"sys":"pod":"n","dt_txt":"2021-12-21 15:00:00","dt":1640109600,"main":"temp":41.58,"feels_like":41.58,"temp_min":41.58,"temp_max":41.58,"pressure":1019,"sea_level":1019,"grnd_level":1012,"humidity":87,"temp_kf":0,"weather":["id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"],"clouds":"all":100,"wind":"speed":1.68,"deg":66,"gust":2.66,"visibility":10000,"pop":0,"sys":"pod":"d","dt_txt":"2021-12-21 18:00:00","dt":1640120400,"main":"temp":44.89,"feels_like":44.89,"temp_min":44.89,"temp_max":44.89,"pressure":1017,"sea_level":1017,"grnd_level":1010,"humidity":82,"temp_kf":0,"weather":["id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"],"clouds":"all":100,"wind":"speed":2.33,"deg":57,"gust":3.49,"visibility":10000,"pop":0,"sys":"pod":"d","dt_txt":"2021-12-21 21:00:00","dt":1640131200,"main":"temp":43.52,"feels_like":43.52,"temp_min":43.52,"temp_max":43.52,"pressure":1016,"sea_level":1016,"grnd_level":1009,"humidity":89,"temp_kf":0,"weather":["id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"],"clouds":"all":100,"wind":"speed":1.83,"deg":190,"gust":1.9,"visibility":10000,"pop":0,"sys":"pod":"d","dt_txt":"2021-12-22 00:00:00","dt":1640142000,"main":"temp":40.98,"feels_like":40.98,"temp_min":40.98,"temp_max":40.98,"pressure":1014,"sea_level":1014,"grnd_level":1007,"humidity":95,"temp_kf":0,"weather":["id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"],"clouds":"all":97,"wind":"speed":1.63,"deg":162,"gust":1.77,"visibility":10000,"pop":0,"sys":"pod":"n","dt_txt":"2021-12-22 03:00:00","dt":1640152800,"main":"temp":42.48,"feels_like":40.23,"temp_min":42.48,"temp_max":42.48,"pressure":1014,"sea_level":1014,"grnd_level":1007,"humidity":98,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10n"],"clouds":"all":99,"wind":"speed":3.94,"deg":175,"gust":12.62,"visibility":10000,"pop":0.74,"rain":"3h":0.97,"sys":"pod":"n","dt_txt":"2021-12-22 06:00:00","dt":1640163600,"main":"temp":45.55,"feels_like":39.47,"temp_min":45.55,"temp_max":45.55,"pressure":1012,"sea_level":1012,"grnd_level":1005,"humidity":93,"temp_kf":0,"weather":["id":501,"main":"Rain","description":"moderate rain","icon":"10n"],"clouds":"all":100,"wind":"speed":13.13,"deg":193,"gust":29.59,"visibility":10000,"pop":1,"rain":"3h":4,"sys":"pod":"n","dt_txt":"2021-12-22 09:00:00","dt":1640174400,"main":"temp":45.1,"feels_like":39.79,"temp_min":45.1,"temp_max":45.1,"pressure":1012,"sea_level":1012,"grnd_level":1005,"humidity":91,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10n"],"clouds":"all":100,"wind":"speed":10.49,"deg":196,"gust":26.4,"visibility":10000,"pop":1,"rain":"3h":1.57,"sys":"pod":"n","dt_txt":"2021-12-22 12:00:00","dt":1640185200,"main":"temp":45.77,"feels_like":41.02,"temp_min":45.77,"temp_max":45.77,"pressure":1011,"sea_level":1011,"grnd_level":1004,"humidity":89,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10n"],"clouds":"all":100,"wind":"speed":9.4,"deg":193,"gust":27.07,"visibility":10000,"pop":0.59,"rain":"3h":0.33,"sys":"pod":"n","dt_txt":"2021-12-22 15:00:00","dt":1640196000,"main":"temp":46.09,"feels_like":39.97,"temp_min":46.09,"temp_max":46.09,"pressure":1010,"sea_level":1010,"grnd_level":1003,"humidity":92,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10d"],"clouds":"all":100,"wind":"speed":13.76,"deg":186,"gust":25.66,"visibility":6452,"pop":1,"rain":"3h":2.72,"sys":"pod":"d","dt_txt":"2021-12-22 18:00:00","dt":1640206800,"main":"temp":46.15,"feels_like":39.67,"temp_min":46.15,"temp_max":46.15,"pressure":1007,"sea_level":1007,"grnd_level":1000,"humidity":89,"temp_kf":0,"weather":["id":501,"main":"Rain","description":"moderate rain","icon":"10d"],"clouds":"all":100,"wind":"speed":15.19,"deg":187,"gust":28.77,"visibility":10000,"pop":1,"rain":"3h":6.36,"sys":"pod":"d","dt_txt":"2021-12-22 21:00:00","dt":1640217600,"main":"temp":45.7,"feels_like":39.43,"temp_min":45.7,"temp_max":45.7,"pressure":1005,"sea_level":1005,"grnd_level":998,"humidity":90,"temp_kf":0,"weather":["id":501,"main":"Rain","description":"moderate
rain","icon":"10d"],"clouds":"all":100,"wind":"speed":13.89,"deg":187,"gust":27.69,"visibility":10000,"pop":1,"rain":"3h":3.19,"sys":"pod":"d","dt_txt":"2021-12-23 00:00:00","dt":1640228400,"main":"temp":45.64,"feels_like":39.24,"temp_min":45.64,"temp_max":45.64,"pressure":1002,"sea_level":1002,"grnd_level":995,"humidity":87,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10n"],"clouds":"all":100,"wind":"speed":14.34,"deg":184,"gust":29.01,"visibility":10000,"pop":0.62,"rain":"3h":0.21,"sys":"pod":"n","dt_txt":"2021-12-23 03:00:00","dt":1640239200,"main":"temp":46.45,"feels_like":39.87,"temp_min":46.45,"temp_max":46.45,"pressure":999,"sea_level":999,"grnd_level":992,"humidity":84,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10n"],"clouds":"all":100,"wind":"speed":15.93,"deg":179,"gust":38.97,"visibility":10000,"pop":0.74,"rain":"3h":0.6,"sys":"pod":"n","dt_txt":"2021-12-23 06:00:00","dt":1640250000,"main":"temp":41.74,"feels_like":35.65,"temp_min":41.74,"temp_max":41.74,"pressure":999,"sea_level":999,"grnd_level":993,"humidity":91,"temp_kf":0,"weather":["id":501,"main":"Rain","description":"moderate rain","icon":"10n"],"clouds":"all":100,"wind":"speed":10.33,"deg":245,"gust":22.88,"visibility":5223,"pop":1,"rain":"3h":5.69,"sys":"pod":"n","dt_txt":"2021-12-23 09:00:00","dt":1640260800,"main":"temp":39.78,"feels_like":34.34,"temp_min":39.78,"temp_max":39.78,"pressure":1000,"sea_level":1000,"grnd_level":993,"humidity":92,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10n"],"clouds":"all":100,"wind":"speed":7.96,"deg":213,"gust":20.74,"visibility":10000,"pop":1,"rain":"3h":1.46,"sys":"pod":"n","dt_txt":"2021-12-23 12:00:00","dt":1640271600,"main":"temp":39.96,"feels_like":34.29,"temp_min":39.96,"temp_max":39.96,"pressure":1000,"sea_level":1000,"grnd_level":993,"humidity":92,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10n"],"clouds":"all":95,"wind":"speed":8.5,"deg":195,"gust":17.92,"visibility":10000,"pop":0.68,"rain":"3h":0.24,"sys":"pod":"n","dt_txt":"2021-12-23 15:00:00","dt":1640282400,"main":"temp":39.81,"feels_like":33.21,"temp_min":39.81,"temp_max":39.81,"pressure":1001,"sea_level":1001,"grnd_level":994,"humidity":93,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10d"],"clouds":"all":91,"wind":"speed":10.47,"deg":218,"gust":19.17,"visibility":10000,"pop":1,"rain":"3h":0.96,"sys":"pod":"d","dt_txt":"2021-12-23 18:00:00","dt":1640293200,"main":"temp":43.43,"feels_like":38.03,"temp_min":43.43,"temp_max":43.43,"pressure":1001,"sea_level":1001,"grnd_level":994,"humidity":78,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10d"],"clouds":"all":99,"wind":"speed":9.66,"deg":244,"gust":19.75,"visibility":10000,"pop":1,"rain":"3h":0.8,"sys":"pod":"d","dt_txt":"2021-12-23 21:00:00","dt":1640304000,"main":"temp":41.31,"feels_like":35.13,"temp_min":41.31,"temp_max":41.31,"pressure":1001,"sea_level":1001,"grnd_level":994,"humidity":74,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10d"],"clouds":"all":76,"wind":"speed":10.33,"deg":243,"gust":23.51,"visibility":10000,"pop":1,"rain":"3h":0.66,"sys":"pod":"d","dt_txt":"2021-12-24 00:00:00","dt":1640314800,"main":"temp":39,"feels_like":32.31,"temp_min":39,"temp_max":39,"pressure":1002,"sea_level":1002,"grnd_level":995,"humidity":85,"temp_kf":0,"weather":["id":803,"main":"Clouds","description":"broken clouds","icon":"04n"],"clouds":"all":76,"wind":"speed":10.22,"deg":218,"gust":22.88,"visibility":10000,"pop":0.58,"sys":"pod":"n","dt_txt":"2021-12-24 03:00:00","dt":1640325600,"main":"temp":38.26,"feels_like":31.3,"temp_min":38.26,"temp_max":38.26,"pressure":1001,"sea_level":1001,"grnd_level":994,"humidity":96,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10n"],"clouds":"all":88,"wind":"speed":10.42,"deg":200,"gust":22.82,"visibility":899,"pop":0.83,"rain":"3h":0.57,"sys":"pod":"n","dt_txt":"2021-12-24 06:00:00","dt":1640336400,"main":"temp":38.89,"feels_like":31.21,"temp_min":38.89,"temp_max":38.89,"pressure":999,"sea_level":999,"grnd_level":992,"humidity":97,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10n"],"clouds":"all":100,"wind":"speed":12.66,"deg":190,"gust":26.53,"visibility":72,"pop":1,"rain":"3h":2.06,"sys":"pod":"n","dt_txt":"2021-12-24 09:00:00","dt":1640347200,"main":"temp":38.59,"feels_like":29.48,"temp_min":38.59,"temp_max":38.59,"pressure":995,"sea_level":995,"grnd_level":988,"humidity":96,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10n"],"clouds":"all":100,"wind":"speed":16.8,"deg":177,"gust":35.63,"visibility":155,"pop":1,"rain":"3h":2.33,"sys":"pod":"n","dt_txt":"2021-12-24 12:00:00","dt":1640358000,"main":"temp":36.75,"feels_like":30.02,"temp_min":36.75,"temp_max":36.75,"pressure":996,"sea_level":996,"grnd_level":989,"humidity":97,"temp_kf":0,"weather":["id":501,"main":"Rain","description":"moderate rain","icon":"10n"],"clouds":"all":100,"wind":"speed":9.17,"deg":197,"gust":19.89,"visibility":293,"pop":1,"rain":"3h":3.56,"sys":"pod":"n","dt_txt":"2021-12-24 15:00:00","dt":1640368800,"main":"temp":39.24,"feels_like":32.86,"temp_min":39.24,"temp_max":39.24,"pressure":997,"sea_level":997,"grnd_level":990,"humidity":92,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10d"],"clouds":"all":100,"wind":"speed":9.64,"deg":184,"gust":22.73,"visibility":7697,"pop":1,"rain":"3h":1.02,"sys":"pod":"d","dt_txt":"2021-12-24 18:00:00","dt":1640379600,"main":"temp":40.75,"feels_like":33.19,"temp_min":40.75,"temp_max":40.75,"pressure":995,"sea_level":995,"grnd_level":988,"humidity":91,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10d"],"clouds":"all":100,"wind":"speed":13.76,"deg":191,"gust":30.6,"visibility":7140,"pop":1,"rain":"3h":1.13,"sys":"pod":"d","dt_txt":"2021-12-24 21:00:00","dt":1640390400,"main":"temp":40.42,"feels_like":33.6,"temp_min":40.42,"temp_max":40.42,"pressure":998,"sea_level":998,"grnd_level":991,"humidity":79,"temp_kf":0,"weather":["id":500,"main":"Rain","description":"light rain","icon":"10d"],"clouds":"all":96,"wind":"speed":11.41,"deg":252,"gust":27.09,"visibility":10000,"pop":1,"rain":"3h":2.4,"sys":"pod":"d","dt_txt":"2021-12-25 00:00:00","dt":1640401200,"main":"temp":37.81,"feels_like":32.34,"temp_min":37.81,"temp_max":37.81,"pressure":1001,"sea_level":1001,"grnd_level":994,"humidity":89,"temp_kf":0,"weather":["id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"],"clouds":"all":98,"wind":"speed":7.27,"deg":204,"gust":18.72,"visibility":10000,"pop":0.54,"sys":"pod":"n","dt_txt":"2021-12-25 03:00:00","dt":1640412000,"main":"temp":36.86,"feels_like":30.6,"temp_min":36.86,"temp_max":36.86,"pressure":1002,"sea_level":1002,"grnd_level":995,"humidity":93,"temp_kf":0,"weather":["id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"],"clouds":"all":98,"wind":"speed":8.3,"deg":195,"gust":17.76,"visibility":10000,"pop":0.5,"sys":"pod":"n","dt_txt":"2021-12-25 06:00:00","dt":1640422800,"main":"temp":36,"feels_like":29.88,"temp_min":36,"temp_max":36,"pressure":1003,"sea_level":1003,"grnd_level":996,"humidity":96,"temp_kf":0,"weather":["id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"],"clouds":"all":96,"wind":"speed":7.72,"deg":203,"gust":16.89,"visibility":387,"pop":0.46,"sys":"pod":"n","dt_txt":"2021-12-25 09:00:00","dt":1640433600,"main":"temp":36.7,"feels_like":31.66,"temp_min":36.7,"temp_max":36.7,"pressure":1003,"sea_level":1003,"grnd_level":996,"humidity":92,"temp_kf":0,"weather":["id":600,"main":"Snow","description":"light snow","icon":"13n"],"clouds":"all":97,"wind":"speed":6.24,"deg":205,"gust":13.78,"visibility":10000,"pop":0.5,"snow":"3h":0.17,"sys":"pod":"n","dt_txt":"2021-12-25 12:00:00","dt":1640444400,"main":"temp":35.83,"feels_like":29.93,"temp_min":35.83,"temp_max":35.83,"pressure":1003,"sea_level":1003,"grnd_level":996,"humidity":91,"temp_kf":0,"weather":["id":803,"main":"Clouds","description":"broken clouds","icon":"04n"],"clouds":"all":72,"wind":"speed":7.29,"deg":200,"gust":17.78,"visibility":10000,"pop":0.53,"sys":"pod":"n","dt_txt":"2021-12-25 15:00:00","dt":1640455200,"main":"temp":36.86,"feels_like":30.34,"temp_min":36.86,"temp_max":36.86,"pressure":1005,"sea_level":1005,"grnd_level":998,"humidity":96,"temp_kf":0,"weather":["id":600,"main":"Snow","description":"light snow","icon":"13d"],"clouds":"all":86,"wind":"speed":8.77,"deg":206,"gust":19.28
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at IncomingMessage.<anonymous> (C:\Users\pc\documents\webdev\theCompleteWebDevBootcamp-AngelaYu\apis\weatherproject\app.js:13:38)
at IncomingMessage.emit (node:events:390:28)
at IncomingMessage.Readable.read (node:internal/streams/readable:527:10)
at flow (node:internal/streams/readable:1012:34)
at resume_ (node:internal/streams/readable:993:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
[nodemon] app crashed - waiting for file changes before starting...
【问题讨论】:
是的,错误是有道理的。您正在尝试仅解析整体响应的一小部分。 【参考方案1】:data
事件触发每次你得到一个块数据。
当您拥有所有数据时,它不会触发一次。
您正在尝试解析 JSON 的 片段,而不是整个 JSON 文本。
看this example in the documentation:
res.on('data', (chunk) => rawData += chunk; ); res.on('end', () => try const parsedData = JSON.parse(rawData); console.log(parsedData); catch (e) console.error(e.message); );
每次收到data
事件时,将其添加到您已有的数据中。当你收到end
事件时,尝试解析它。
直接使用http
和https
模块很痛苦。他们不是很友好。我建议使用带有更友好 API 的包装器,例如 axios 或 node-fetch(两者都返回一个在所有数据可用时解析的承诺)。
【讨论】:
以上是关于SyntaxError:JSON.parse 处的 JSON 输入意外结束 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
DiscordJS + NodeJS:SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous>)
SyntaxError:JSON.parse (<anonymous>) 处的 JSON 输入意外结束
NodeJs JSON.parse-SyntaxError:JSON中位置0处的意外令牌u
在 JSON.parse 的位置 0 处获取“SyntaxError:”JSON 中的意外标记”