繁体   English   中英

JSON.parse() 出现意外错误:“JSON 输入意外结束”

[英]JSON.parse() is giving unexpected error : "Unexpected end of JSON input"

控制台.log(d); 给出以下 output (十六进制代码):

< 缓冲器 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 6f 323 21 39 6c 3 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 6e 22 3a 366626 38 6f 34 6 26 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 66 69 67 6e 22 3a 31 2c 22 64 69 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 656393 28 3 3a 33 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 6676 22 6e 2c 6 6d 65... 740 更多字节>

当我尝试使用 JSON.parse(d) 将其转换为 JSON 时,出现以下错误:

未定义:1

{"success":true,"data":{"summary":{"total":19925604,"confirmedCasesIndian":19925556,"confirmedCasesForeign":48,"discharged":16293003,"deaths":218959,"confirmedButLocationUnidentified" :0},"unofficial-summary":[{"source":"covid19india.org","total":7945975,"recovered":7198877,"deaths":119538,"active":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");
})

method.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 (和/或axios )而不是 https,因为它是常用的(并且更容易,因为您不必检查数据的开始或结束)。

const fetch = require('node-fetch')

......

const results = await fetch(url).then(res => res.json());
console.log(results)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM