Filippo oretti
Filippo oretti

Reputation: 49817

Can't parse json returned by Openstreetmap

i'm not able to parse this 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"}]

I'm trying JSON.parse(_json); it returns in console: SyntaxError: JSON.parse: unexpected character

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

i need the lat & long values.

Upvotes: 0

Views: 859

Answers (2)

Shehabic
Shehabic

Reputation: 6877

Well this is not a json string

it's just a JS array with objects

You can simply use it like that:

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

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

Upvotes: 3

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382102

You var _json isn't JSON : it's a standard javascript array you can use without parsing.

If you want the first latitude, simply do

var lat = _json[0].lat;

But I'd suggest giving a better name than _json, for example places as this array contain places.

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

Upvotes: 1

Related Questions