繁体   English   中英

SyntaxError:JSON解析错误:尝试解析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) } 

这里有一些问题:

  1. location是JavaScript中的关键字,您不能将其作为参数传递给函数。

  2. location" Antoine Vallasois Ave, Vacoas-Phoenix, England", "stopover":true不是有效的JSON,因此会给您一个错误。

  3. 您没有声明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)

}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM