node.js - JSON.parse returns [object] from JSON -
i'm using npm package called request
make http request https://maps.googleapis.com/maps/api/geocode/json?address=jur%c4%8dkova+cesta+225&key=aizasychkpdcaaavzwyof8zbkspokuyt41nlj_0
now want parse data receive in order extract lat/long , write database. far output console is:
[ { address_components: [ [object], [object], [object], [object], [object], [object], [object] ], formatted_address: 'breznikova ulica 15, 1230 domžale, slovenia', geometry: { location: [object], location_type: 'rooftop', viewport: [object] }, place_id: 'chijr2mtduc0zucrv5nxk0zex7m', types: [ 'street_address' ] }, { address_components: [ [object], [object], [object], [object], [object], [object], [object] ], formatted_address: 'breznikova ulica 15, 1000 ljubljana, slovenia', geometry: { location: [object], location_type: 'rooftop', viewport: [object] }, place_id: 'chij19ax9opmekcropvkj6skneg', types: [ 'street_address' ] }, { address_components: [ [object], [object], [object], [object], [object], [object], [object] ], formatted_address: 'breznikova ulica 15, 2000 maribor, slovenia', geometry: { location: [object], location_type: 'rooftop', viewport: [object] }, place_id: 'chijduicxfd2b0cryrf99vgtpbs', types: [ 'street_address' ] } ]
here's extract of code produces server.js file:
//get lat , long before saving gmaps api //build gmaps api url var urladdress = shop.address.replace(/ /gi, '+'); var urlapikey = '&key=aizasychkpdcaaavzwyof8zbkspokuyt41nlj_0'; var url = 'https://maps.googleapis.com/maps/api/geocode/json?address='; url = url.concat(urladdress).concat(urlapikey); //make request request({ uri: url, method:"get", timeout: 100000 }, function(error, response, body) { var gmaps = json.parse(body); console.log(gmaps.results); });
if can point out i'm doing wrong great. if try output object returns undefined.
the reason why appears [object] readability, if want show full dump of results, have pass through json.stringify first.
as access address_components, results array, you'll have access them specific element of array (e.g. gmaps.results[0].address_components
)
Comments
Post a Comment