简体   繁体   English

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

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

console.log(d);控制台.log(d); gives the following output (hexadecimal code):给出以下 output (十六进制代码):

<Buffer 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 74 61 6c 22 3a 31 39 39 32 35 36 30... 293 more bytes> < 缓冲器 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 更多字节>

<Buffer 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 36 30 38 34 2c 22 63 6f 6e 66 69 72 6d... 1319 more bytes> <Buffer 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 65 69 67 6e 22 3a 31 2c 22 64 69 73... 1319 more bytes> < 缓冲器 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 更多字节>

<Buffer 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 65 64 22 3a 33 39 38 31 36 35 38 2c 22... 1319 more bytes> < 缓冲区 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 更多字节>

<Buffer 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 61 22 2c 22 63 6f 6e 66 69 72 6d 65... 740 more bytes> < 缓冲器 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 更多字节>

When I try to convert this into JSON using JSON.parse(d), I get the following error:当我尝试使用 JSON.parse(d) 将其转换为 JSON 时,出现以下错误:

undefined:1未定义: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":"Andaman and Nicoba {"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)

Here is my app.js file code:这是我的 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");
})

The method.on("data") receives chunks of buffers, you need to concat that buffers once receiving is finished, convert to string and then parse to JSON. 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);
    });
})

Even though the question is about https即使问题是关于https

To make things easier, may I suggest to use node-fetch (and/or axios ) instead of https, since it's commonly used (and easier since you don't have to check for start or end of data).为了让事情变得更简单,我可以建议使用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.

相关问题 JSON.parse() 返回意外的输入结束 - JSON.parse() Returning Unexpected end of input SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous> - SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) 语法错误:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) on(&quot;数据&quot;) - SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) on("data") JAVASCRIPT JSON.parse() 返回 JSON 输入的意外结束 - JAVASCRIPT JSON.parse() return Unexpected end of JSON input JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous> - Unexpected end of JSON input at JSON.parse (<anonymous>) Json.parse 抛出:“语法错误:JSON 输入的意外结束” - Json.parse throws: “SyntaxError: Unexpected end of JSON input” json.parse意外令牌错误 - json.parse unexpected token error JSON.parse 导致 json 输入意外结束,但我的 json 是正确的 - JSON.parse cause unexpected end of json input but my json is correct 如何解决 SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous> ) 同时在 NodeJs 中获取 JSON 数据</anonymous> - How to solve SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) while fetching JSON data in NodeJs 在 JSON.parse(<anonymous> ) 在发出 HTTPS GET 请求时</anonymous> - Unexpected end of JSON input at JSON.parse(<anonymous>) While making a HTTPS GET Request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM