簡體   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