JSON.parse() is giving unexpected error : "Unexpected end of JSON input"
JSON.parse() is giving unexpected error : "Unexpected end of JSON input"
console.log(d);给出以下输出(十六进制代码):
<缓冲区 7b 22 73 75 63 63 65 73 73 22 3a 74 72 75 65 2c 22 64 61 74 61 22 3a 7b 22 73 75 6d 6d 61 72 79 22 3a 7b 22 74 6cf 36 4 22 22 39 39 32 35 36 30 ... 293 更多字节>
<缓冲区 6f 62 61 72 20 49 73 6c 61 6e 64 73 22 2c 22 63 6f 6e 66 69 72 6d 65 64 43 61 73 65 73 49 6e 64 69 61 6f 22 3a 30 6e 22 3 66 69 72 6d ... 1319 更多字节>
<缓冲区 43 61 73 65 73 49 6e 64 69 61 6e 22 3a 35 39 34 36 30 31 2c 22 63 6f 6e 66 69 72 6d 65 64 43 61 73 65 73 46 6f 72 2 65 4 6a 3 73 ... 1319 更多字节>
<缓冲区 38 2c 22 63 6f 6e 66 69 72 6d 65 64 43 61 73 65 73 46 6f 72 65 69 67 6e 22 3a 33 2c 22 64 69 73 63 68 61 72 67 234 64 3a 36 35 38 2c 22 ... 1319 更多字节>
<缓冲区 61 6c 43 6f 6e 66 69 72 6d 65 64 22 3a 31 32 30 37 31 31 32 7d 2c 7b 22 6c 6f 63 22 3a 22 54 65 6c 61 6e 67 61 6e 221 6f 6f 69 72 6d 65 ... 740 更多字节>
当我尝试使用 JSON.parse(d) 将其转换为 JSON 时,出现以下错误:
未定义:1
{“成功”:true,“数据”:{“摘要”:{“总计”:19925604,“confirmedCasesIndian”:19925556,“confirmedCasesForeign”:48,“出院”:16293003,“死亡”: 218959,“confirmedButLocationUnidentified”:0},“非官方摘要”:[{“来源”:“covid19india.org”,“总计”:7945975,“恢复”:7198877,“死亡”:119538,“活动” :626192}],"regional":[{"loc":"安达曼和尼科巴
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at IncomingMessage.<anonymous> (E:\Web Development\Test\app.js:16:24)
at IncomingMessage.emit (events.js:315:20)
at IncomingMessage.Readable.read (internal/streams/readable.js:519:10)
at flow (internal/streams/readable.js:992:34)
at resume_ (internal/streams/readable.js:973:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
这是我的 app.js 文件代码:
const express = require("express");
const https = require("https");
const app = express();
app.get("/", (req, res) => {
const url = "https://api.rootnet.in/covid19-in/stats/latest";
https.get(url, function(response) {
console.log(response.statusCode);
response.on("data", (d) => {
console.log(JSON.parse(d));
// console.log(d);
})
res.send()
})
})
app.listen(3000, () => {
console.log("server up and running at port 3000");
})
.on("data") 方法接收缓冲区块,您需要在接收完成后连接缓冲区,转换为字符串,然后解析为 JSON.
https.get(url, function(response) {
const data = [];
response.on("data", (d) => {
data.push(d);
}).on('end', function() {
//at this point data is an array of Buffers
//so Buffer.concat() can make us a new Buffer
//of all of them together
const buffer = Buffer.concat(data);
const obj = JSON.parse(buffer.toString());
console.log(obj);
});
})
尽管问题是关于 https
为了使事情更简单,我建议使用 node-fetch (and/or axios) 而不是 https,因为它很常用(而且更容易,因为您不必检查数据的开始或结束)。
const fetch = require('node-fetch')
......
const results = await fetch(url).then(res => res.json());
console.log(results)
console.log(d);给出以下输出(十六进制代码):
<缓冲区 7b 22 73 75 63 63 65 73 73 22 3a 74 72 75 65 2c 22 64 61 74 61 22 3a 7b 22 73 75 6d 6d 61 72 79 22 3a 7b 22 74 6cf 36 4 22 22 39 39 32 35 36 30 ... 293 更多字节>
<缓冲区 6f 62 61 72 20 49 73 6c 61 6e 64 73 22 2c 22 63 6f 6e 66 69 72 6d 65 64 43 61 73 65 73 49 6e 64 69 61 6f 22 3a 30 6e 22 3 66 69 72 6d ... 1319 更多字节> <缓冲区 43 61 73 65 73 49 6e 64 69 61 6e 22 3a 35 39 34 36 30 31 2c 22 63 6f 6e 66 69 72 6d 65 64 43 61 73 65 73 46 6f 72 2 65 4 6a 3 73 ... 1319 更多字节>
<缓冲区 38 2c 22 63 6f 6e 66 69 72 6d 65 64 43 61 73 65 73 46 6f 72 65 69 67 6e 22 3a 33 2c 22 64 69 73 63 68 61 72 67 234 64 3a 36 35 38 2c 22 ... 1319 更多字节>
<缓冲区 61 6c 43 6f 6e 66 69 72 6d 65 64 22 3a 31 32 30 37 31 31 32 7d 2c 7b 22 6c 6f 63 22 3a 22 54 65 6c 61 6e 67 61 6e 221 6f 6f 69 72 6d 65 ... 740 更多字节>
当我尝试使用 JSON.parse(d) 将其转换为 JSON 时,出现以下错误:
未定义:1
{“成功”:true,“数据”:{“摘要”:{“总计”:19925604,“confirmedCasesIndian”:19925556,“confirmedCasesForeign”:48,“出院”:16293003,“死亡”: 218959,“confirmedButLocationUnidentified”:0},“非官方摘要”:[{“来源”:“covid19india.org”,“总计”:7945975,“恢复”:7198877,“死亡”:119538,“活动” :626192}],"regional":[{"loc":"安达曼和尼科巴
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at IncomingMessage.<anonymous> (E:\Web Development\Test\app.js:16:24)
at IncomingMessage.emit (events.js:315:20)
at IncomingMessage.Readable.read (internal/streams/readable.js:519:10)
at flow (internal/streams/readable.js:992:34)
at resume_ (internal/streams/readable.js:973:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
这是我的 app.js 文件代码:
const express = require("express");
const https = require("https");
const app = express();
app.get("/", (req, res) => {
const url = "https://api.rootnet.in/covid19-in/stats/latest";
https.get(url, function(response) {
console.log(response.statusCode);
response.on("data", (d) => {
console.log(JSON.parse(d));
// console.log(d);
})
res.send()
})
})
app.listen(3000, () => {
console.log("server up and running at port 3000");
})
.on("data") 方法接收缓冲区块,您需要在接收完成后连接缓冲区,转换为字符串,然后解析为 JSON.
https.get(url, function(response) {
const data = [];
response.on("data", (d) => {
data.push(d);
}).on('end', function() {
//at this point data is an array of Buffers
//so Buffer.concat() can make us a new Buffer
//of all of them together
const buffer = Buffer.concat(data);
const obj = JSON.parse(buffer.toString());
console.log(obj);
});
})
尽管问题是关于 https
为了使事情更简单,我建议使用 node-fetch (and/or axios) 而不是 https,因为它很常用(而且更容易,因为您不必检查数据的开始或结束)。
const fetch = require('node-fetch')
......
const results = await fetch(url).then(res => res.json());
console.log(results)