无法解析Openstreetmap返回的json

时间:2012-10-07 11:24:03

标签: json parsing openstreetmap

我无法解析这个json:

var _json =   [{"place_id":"18094048","licence":"Data \u00a9 OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright","osm_type":"node","osm_id":"1695627257","boundingbox":[34.549406280518,34.569410095215,135.45611022949,135.47612548828],"lat":"34.5594098","lon":"135.4661246","display_name":"Singapore Embassy, \u583a\u72ed\u5c71\u7dda (Sakai-Sayama line), Sakai, Senboku District, Kinki Region, Giappone","class":"amenity","type":"embassy"},

"place_id":"17954461","licence":"Data \u00a9 OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright","osm_type":"node","osm_id":"1695740584","boundingbox":

[35.647695770264,35.667699584961,139.72419189453,139.74420715332],"lat":"35.6576973","lon":"139.7341957","display_name":"Singapore Embassy, Gaien higashi dori, Roppongi, Minato, \u5317\u8db3\u7acb\u90e1, 1080074, Giappone","class":"amenity","type":"embassy"}]

我正在尝试JSON.parse(_json);它在控制台中返回: SyntaxError:JSON.parse:意外字符

console.log(JSON.parse(_json));

我需要lat&很长的价值观。

2 个答案:

答案 0 :(得分:3)

这不是一个json字符串

它只是一个带有对象的JS数组

你可以这样简单地使用它:

for(var i = 0 ; i < _json.length ; i++ ){
   var jsonObject  = _json[i];

   // then just use jsonObject['lat'] , jsonObject['license'] .....etc
}

答案 1 :(得分:1)

你var _json不是JSON:它是一个标准的javascript数组,你可以在不解析的情况下使用。

如果你想要第一个纬度,只需做

var lat = _json[0].lat;

但我建议提供比_json更好的名称,例如places,因为此数组包含地点。

var firstPlace = _json[0]; // firstPlace has properties named lat and lon