简体   繁体   English

SyntaxError:JSON解析错误:尝试解析JSON时出现意外的标识符“对象”

[英]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: 这里有一些问题:

  1. location is a keyword in JavaScript, you cannot pass that as parameter to a function. location是JavaScript中的关键字,您不能将其作为参数传递给函数。

  2. 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,因此会给您一个错误。

  3. 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.

相关问题 SyntaxError:JSON解析错误:意外的标识符“object” - SyntaxError: JSON Parse error: Unexpected identifier “object” SyntaxError:JSON解析错误:意外的标识符“功能” - SyntaxError: JSON Parse error: Unexpected identifier “function” SyntaxError:JSON解析错误:意外的标识符“对象”(匿名函数) - SyntaxError: JSON Parse error: Unexpected identifier “object” (anonymous function) JSON 解析错误:尝试解析数组中的对象时出现意外的标识符“未定义”(React-Native) - JSON Parse error: Unexpected identifier "undefined" when trying to parse an object from an array (React-Native) 尝试解析简单的json对象时,为什么JSON.parse()抛出Uncaught SyntaxError:Unexpected token? - Why does JSON.parse() throw Uncaught SyntaxError: Unexpected token when trying to parse a simple json object? JSON 解析错误:意外的标识符“对象” - JSON parse error: Unexpected identifier “object” JSON 解析错误:意外的标识符“be” - JSON Parse error: Unexpected identifier "be" 尝试使用转义单引号解析 JSON 时,Javascript 中出现“意外标识符”错误 - "Unexpected identifier" error in Javascript when trying to parse JSON with escaped single quote JSON解析错误:意外的标识符“ Try” - JSON Parse error: Unexpected identifier “Try” "JSON 解析错误:意外的标识符“未定义”" - JSON Parse error: Unexpected identifier "undefined"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM