【问题标题】:JSON.parse() is giving unexpected error : "Unexpected end of JSON input"JSON.parse() 出现意外错误:“JSON 输入意外结束”
【发布时间】:2021-07-26 01:54:19
【问题描述】:

console.log(d);给出以下输出(十六进制代码):

当我尝试使用 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");
})

【问题讨论】:

  • response.on("data", (d) =&gt; { console.log(JSON.parse(d)); } - d 是一个“块”数据,您可能没有收到一个块中的完整数据,但您正在尝试解析它。如果它不是完整的 json 有效负载,则解析将失败,因为它不是有效的 json。等待“结束”事件,然后合并缓冲区并解析。

标签: node.js json api express https


【解决方案1】:

.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);
    });
})

【讨论】:

  • Console.log() 给出了一个奇怪的输出,我期待一个 JSON 输出,如:{ "success": true, "data": { "summary": { "total": 19925604, "confirmedCasesIndian “:19925556,“confirmedCasesForeign”:48,“出院”:16293003,“死亡”:218959,“confirmedButLocationUnidentified”:0},
  • 但我得到了这种类型的输出:
  • {type: 'Buffer',data: [123, 34, 115, 117, 99, 99, 101, 115, 115, 34, 58, 116, 114, 117, 101, 44, 34, 100, 97, 116, 97, 34, 58, 123, 34, 115, 117, 109, 109, 97, 114, 121, 34, 58, 123, 34, 116, 111, 116, 97, 108, 34, 58, 49, 57, 57, 50, 53, 54, 48, 52, 44, 34, 99, 111, 110, 102, 105, 114, 109, 101, 100, 67, 97, 115, 101, 115, 73, 110, 100, 105, 97, 110, 34, 58, 49, 57, 57, 50, 53, 53, 53, 54, 44, 34, 99, 111, 110, 102, 105, 114, 109, 101, 100, 67, 97, 115, 101, 115, 70, ... 还有 236 项] }
  • 我的错误......现在我明白了,请检查我的更新答案
  • 为什么我需要做这一切,因为当我打开 API 链接(api.rootnet.in/covid19-in/stats/latest)时,数据是 JSON 文件?为什么我不能直接获取 Hexa 编码的 JSON 对象并直接使用 JSON.parse() 进行转换?
【解决方案2】:

即使问题是关于https

为了让事情更简单,我建议使用node-fetch(和/或axios)而不是https,因为它很常用(而且更容易,因为您不必检查数据的开始或结束) .

const fetch = require('node-fetch')

......

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

【讨论】:

  • 我怎么知道每一个比我在在线课程中学到的图书馆更好的图书馆。你能建议或帮助我如何知道这个库更好、更容易实现。我在哪里可以找到这样更好的库,我的意思是我应该用谷歌搜索这样的东西。 PS:我是后端开发的新手
  • 如果您不知道,请使用我的建议。 node-fetchaxios。我在答案中提供了示例代码。随着您开始编写更多代码,您将了解您使用的常用库,并开始更多地研究您打算做什么。这个时候,你想学习如何从 url 获取数据,你知道 node-fetchaxios :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-01
  • 2021-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-25
相关资源
最近更新 更多