SyntaxError: Unexpected token e in JSON at position 1 with JSON.parse()
SyntaxError: Unexpected token e in JSON at position 1 with JSON.parse()
我正在尝试使用 openweathermap.org 天气 api,我正在获取数据但无法解析它。
这是我的代码:
request("https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric&callback=test", function(error, response, body) {
if (!error && response.statusCode == 200) {
var parsedData = JSON.parse(body);
console.log(typeof body);
}
});
console.log(typeof body);
returns 字符串所以我无法弄清楚问题是什么。
您正在寻找这个 - URL 是 JSONP 所以它需要一个名为 test 的函数,不需要解析
<script>
const test = data => console.log(data);
</script>
<script src="https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric&callback=test"></script>
或者删除回调 - 这里使用 fetch:
fetch("https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric")
.then(response => response.json())
.then(data => console.log(data));
或 Axios
axios.get('https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric')
.then(response => console.log(response.data));
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
因为您使用的 API 具有回调函数 return 作为响应。它不是 JSON 而是 JSONP(JSON 带填充)。
如果您从 URL.
中删除回调参数,您的代码将正常工作
request("https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric", function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response);
}
});
如需进一步了解 JSONP,您可以参考 this。
这对我有用。只需使用 url 和 json 属性 为第一个参数设置对象。设置 json:true
。设置为 true 将为您处理解析:)
const request = require('request');
const url = "https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric";
request({ url: url, json: true }, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});
正如@mplungjan 指出的那样。那是 JSONP url.
删除 URL 末尾的 &callback=test
然后你就不必处理 JSONP,你可以像往常一样使用它。
我正在尝试使用 openweathermap.org 天气 api,我正在获取数据但无法解析它。
这是我的代码:
request("https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric&callback=test", function(error, response, body) {
if (!error && response.statusCode == 200) {
var parsedData = JSON.parse(body);
console.log(typeof body);
}
});
console.log(typeof body);
returns 字符串所以我无法弄清楚问题是什么。
您正在寻找这个 - URL 是 JSONP 所以它需要一个名为 test 的函数,不需要解析
<script>
const test = data => console.log(data);
</script>
<script src="https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric&callback=test"></script>
或者删除回调 - 这里使用 fetch:
fetch("https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric")
.then(response => response.json())
.then(data => console.log(data));
或 Axios
axios.get('https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric')
.then(response => console.log(response.data));
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
因为您使用的 API 具有回调函数 return 作为响应。它不是 JSON 而是 JSONP(JSON 带填充)。 如果您从 URL.
中删除回调参数,您的代码将正常工作request("https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric", function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response);
}
});
如需进一步了解 JSONP,您可以参考 this。
这对我有用。只需使用 url 和 json 属性 为第一个参数设置对象。设置 json:true
。设置为 true 将为您处理解析:)
const request = require('request');
const url = "https://api.openweathermap.org/data/2.5/weather?lat=30.4831&lon=76.595&appid=7beb6de85d3f3a28dabda1015684562f&units=metric";
request({ url: url, json: true }, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});
正如@mplungjan 指出的那样。那是 JSONP url.
删除 URL 末尾的 &callback=test
然后你就不必处理 JSONP,你可以像往常一样使用它。