SyntaxError: JSON Parse error: Unexpected identifier "object" when trying to parse json
SyntaxError: JSON Parse error: Unexpected identifier "object" when trying to parse json
我在尝试将字符串解析为 json
时遇到错误
这是我的字符串
{"location": " Antoine Vallasois Ave, Vacoas-Phoenix, England", "stopover":true}
这是我的 javascript 函数
function fillWaypoints(location){
var ob =JSON.parse(location);
ArrayWaypoints.push(ob)
}
这里有一些问题:
location
是 JavaScript 中的关键字,不能将其作为参数传递给函数。
location
值 " Antoine Vallasois Ave, Vacoas-Phoenix, England", "stopover":true
不是有效的 JSON,所以它会给你一个错误。
您没有声明ArrayWaypoints
。
您可以尝试以下方式:
var loc = {"location": " Antoine Vallasois Ave, Vacoas-Phoenix, England", "stopover":true}
var ArrayWaypoints = [];
function fillWaypoints(loc){
loc.location.split(',').forEach(function(l){
ArrayWaypoints.push(l.trim());
});
}
fillWaypoints(loc);
console.log(ArrayWaypoints);
嘿,请注意这里您正在尝试解析 Json.. 您必须在 JSON.parse() 函数中传递字符串,因为 JSON.parse 只能将字符串解析为 json:-
var a = '{"location": " Antoine Vallasois Ave Vacoas-Phoenix England", "stopover":true}'
let ArrayWaypoints = [];
function fillWaypoints(location){
var ob =JSON.parse(location);
ArrayWaypoints.push(ob)
}
我在尝试将字符串解析为 json
时遇到错误这是我的字符串
{"location": " Antoine Vallasois Ave, Vacoas-Phoenix, England", "stopover":true}
这是我的 javascript 函数
function fillWaypoints(location){
var ob =JSON.parse(location);
ArrayWaypoints.push(ob)
}
这里有一些问题:
location
是 JavaScript 中的关键字,不能将其作为参数传递给函数。location
值" Antoine Vallasois Ave, Vacoas-Phoenix, England", "stopover":true
不是有效的 JSON,所以它会给你一个错误。您没有声明
ArrayWaypoints
。
您可以尝试以下方式:
var loc = {"location": " Antoine Vallasois Ave, Vacoas-Phoenix, England", "stopover":true}
var ArrayWaypoints = [];
function fillWaypoints(loc){
loc.location.split(',').forEach(function(l){
ArrayWaypoints.push(l.trim());
});
}
fillWaypoints(loc);
console.log(ArrayWaypoints);
嘿,请注意这里您正在尝试解析 Json.. 您必须在 JSON.parse() 函数中传递字符串,因为 JSON.parse 只能将字符串解析为 json:-
var a = '{"location": " Antoine Vallasois Ave Vacoas-Phoenix England", "stopover":true}'
let ArrayWaypoints = [];
function fillWaypoints(location){
var ob =JSON.parse(location);
ArrayWaypoints.push(ob)
}