简体   繁体   English

SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) 在传入消息。<anonymous></anonymous></anonymous>

[英]SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at IncomingMessage.<anonymous>

Im trying to make a weather app and I'm using a weather API to get the information but this error shows up when I'm trying to parse the JSON data: SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at IncomingMessage.<anonymous> edit: I need to get the lat and lon values from an https request. Im trying to make a weather app and I'm using a weather API to get the information but this error shows up when I'm trying to parse the JSON data: SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at IncomingMessage.<anonymous>编辑:我需要从 https 请求中获取 lat 和 lon 值。 I tried returning it but it will still say lon and lat is undefined.我尝试返回它,但它仍然会说 lon 和 lat 未定义。

second edit: If I input the values of lon and lat It will parse the data and send back the JSON but I need the values of lon and lat from the locaionIQ API on the https request.第二次编辑:如果我输入 lon 和 lat 的值,它将解析数据并发回 JSON 但我需要 Z5E056C500A501C4B6BADE57180 请求中来自 locaionIQ API 的 lon 和 lat 的值。 How do I get those values?我如何获得这些值? here is the code:这是代码:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const https = require('https');
const { static, response } = require('express');
require('dotenv').config();


app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static('public'))

app.listen(3000, ()=>{
  console.log("server is running on port 3000")
})

app.get('/', (req,res)=>{
  res.sendFile(__dirname+'/index.html')
})

app.post('/', (req,res)=>{
  
  let apiKeyLocationIQ = process.env.API_KEY_LOCATIONIQ;
  let apiKeyWeather = process.env.API_KEY_WEATHER;
  let cityLocationIQ = req.body.citySearch;
  let urlLocationIQ = "https://api.locationiq.com/v1/search.php?format=JSON&key="+apiKeyLocationIQ+"&q="+cityLocationIQ+"&limit=1";
  https.get(urlLocationIQ, function(response){
    response.on("data",function(data){
      let locationIQ = JSON.parse(data);
      const lat= locationIQ[0].lat;
      const lon= locationIQ[0].lon;
      const cityName = locationIQ[0].display_name;
    })
  })

  let urlWeather = 'https://api.openweathermap.org/data/2.5/onecall?&lat='+lat+'&lon='+lon+'&exclude=alerts,minutely&units=metric&appid='+apiKeyWeather+'&lang=en&cnt=7';
  https.get(urlWeather, function(response){
    response.on("data",function(data){
      let weatherData = JSON.parse(data);
      console.log(weatherData);
      
    })
  })
  
})

Under Node, HTTP/HTTPS request data can arrive in multiple chunks which need to be amalgamated before parsing the resultant JSON string.在 Node 下, HTTP/HTTPS请求数据可以以多个块的形式到达,这些块需要在解析生成的 JSON 字符串之前进行合并。

Essentially JSON parsing needs to be performed in an on("end", callback) , not in an on("data", callback) , or risk incomplete JSON text.本质上 JSON 解析需要在on("end", callback)中执行,而不是在on("data", callback)中执行,否则有不完整的 JSON 文本的风险。

Node documentation has an excellent working example for getting JSON data under http.get(options[, callback]) .节点文档有一个很好的工作示例,用于在http.get(options[, callback])下获取 JSON 数据。 This does not appear to be duplicated in the HTTPS section of documentation due to similarities of the HTTP and HTTPS APIs.由于HTTPHTTPS API 的相似性,这在文档的 HTTPS 部分中似乎没有重复。

Https response data are collected in small chunks, so when its collecting data ("on"), we should append the data(string) in variable (starting with empty string [here let chunks="";]) Https 响应数据是按小块收集的,所以当它收集数据时(“on”),我们应该 append 变量中的数据(字符串)(以空字符串开头 [here let chunks="";])

When all the chunks of data are collected ("end"), then we should finally parse the string(API data) into JSON format.当所有数据块都收集完毕(“结束”)后,我们应该最终将字符串(API 数据)解析为 JSON 格式。

https.get(apiLink, function (response) { 

        let chunks="";
        response.on("data", function (chunk) {
            chunks+=chunk;
        });
        response.on("end", function(){
            // console.log("API Data recieved");

            let defaultCovidData = JSON.parse(chunks);
            // code after parse string into JSON format
        });

});

暂无
暂无

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

相关问题 SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous> - SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) 未捕获的 SyntaxError:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) 未捕获的语法错误:JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous> - Uncaught 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") 未捕获的语法错误:JSON 输入的意外结束:在 JSON.parse(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected end of JSON input : at JSON.parse (<anonymous>) 未捕获的语法错误:JSON.parse 处的 JSON 输入意外结束(<anonymous> ) 在 XMLHttpRequest。<anonymous> ((指数):32)</anonymous></anonymous> - Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at XMLHttpRequest.<anonymous> ((index):32) JSON.parse 处的 JSON 输入意外结束(<anonymous> )</anonymous> - Unexpected end of JSON input at JSON.parse (<anonymous>) 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> ) - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM