[英]SyntaxError: JSON Parse error: Unexpected identifier “object” when trying to parse json
I'm getting errors when trying to parse a string to json 尝试将字符串解析为json时出现错误
here is my String 这是我的弦
{"location": " Antoine Vallasois Ave, Vacoas-Phoenix, England", "stopover":true}
and here is my javascript function 这是我的javascript函数
function fillWaypoints(location){ var ob =JSON.parse(location); ArrayWaypoints.push(ob) }
There are some issues here: 这里有一些问题:
location
is a keyword in JavaScript, you cannot pass that as parameter to a function. location
是JavaScript中的关键字,您不能将其作为参数传递给函数。
location
value " Antoine Vallasois Ave, Vacoas-Phoenix, England", "stopover":true
is not a valid JSON, so it will give you an error. location
值" Antoine Vallasois Ave, Vacoas-Phoenix, England", "stopover":true
不是有效的JSON,因此会给您一个错误。
You didn't declare ArrayWaypoints
. 您没有声明
ArrayWaypoints
。
You can try the following way: 您可以尝试以下方式:
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);
Hey Please Note Here You are trying to parse Json.. You have to pass string in JSON.parse() function because JSON.parse can only parse string into json:- 嘿,请注意这里您正在尝试解析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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.