文字列を json に解析しようとするとエラーが発生します
これが私の文字列です
{"location": " Antoine Vallasois Ave, Vacoas-Phoenix, England", "stopover":true}
ここに私のjavascript関数があります
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 のキーワードです。これをパラメーターとして関数に渡すことはできません。
locationvalue" 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);