{"id":7069,"date":"2014-05-19T04:11:49","date_gmt":"2014-05-19T04:11:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/19\/parse-json-from-google-maps-reverse-geocode-collection-of-common-programming-errors\/"},"modified":"2014-05-19T04:11:49","modified_gmt":"2014-05-19T04:11:49","slug":"parse-json-from-google-maps-reverse-geocode-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/19\/parse-json-from-google-maps-reverse-geocode-collection-of-common-programming-errors\/","title":{"rendered":"Parse json from google maps reverse geocode?-Collection of common programming errors"},"content":{"rendered":"<p>How can I parse through the response from a reverse geocode using Google Maps JavaScript API v3.<\/p>\n<pre><code>geocoder.geocode({'latLng': latlng}, function(results, status) {\n                if (status == google.maps.GeocoderStatus.OK) {\n                    if (results[0]) {\n                        infowindow.setContent(results[0].formatted_address);\n                        infowindow.open(map, marker);\n                    }\n                } \n<\/code><\/pre>\n<p>This displays the formatted address fine in the popup, but I&#8217;m trying to take other bits out of the response, ideally the street name or route (if no street name found). But when using <code>obj = JSON.parse(json);<\/code> I keep getting this error in the console.<\/p>\n<blockquote>\n<p>SyntaxError: JSON.parse: unexpected character<\/p>\n<\/blockquote>\n<p>if it were PHP I would do a bunch of <code>for each<\/code> loops. Is it possible to do something similar in JavaScript ?<\/p>\n<p>heres a sample<\/p>\n<pre><code>{\n   \"results\" : [\n  {\n     \"address_components\" : [\n        {\n           \"long_name\" : \"131\",\n           \"short_name\" : \"131\",\n           \"types\" : [ \"street_number\" ]\n        },\n        {\n           \"long_name\" : \"Stubbington Avenue\",\n           \"short_name\" : \"Stubbington Ave\",\n           \"types\" : [ \"route\" ]\n        },\n        {\n           \"long_name\" : \"Portsmouth\",\n           \"short_name\" : \"Portsmouth\",\n           \"types\" : [ \"locality\", \"political\" ]\n        },\n        {\n           \"long_name\" : \"Portsmouth\",\n           \"short_name\" : \"Portsmouth\",\n           \"types\" : [ \"administrative_area_level_3\", \"political\" ]\n        },\n        {\n           \"long_name\" : \"Portsmouth\",\n           \"short_name\" : \"Portsmouth\",\n           \"types\" : [ \"administrative_area_level_2\", \"political\" ]\n        },\n        {\n           \"long_name\" : \"England\",\n           \"short_name\" : \"England\",\n           \"types\" : [ \"administrative_area_level_1\", \"political\" ]\n        },\n        {\n           \"long_name\" : \"United Kingdom\",\n           \"short_name\" : \"GB\",\n           \"types\" : [ \"country\", \"political\" ]\n        },\n        {\n           \"long_name\" : \"PO2\",\n           \"short_name\" : \"PO2\",\n           \"types\" : [ \"postal_code_prefix\", \"postal_code\" ]\n        },\n        {\n           \"long_name\" : \"Portsmouth\",\n           \"short_name\" : \"Portsmouth\",\n           \"types\" : [ \"postal_town\" ]\n        }\n     ],\n     \"formatted_address\" : \"131 Stubbington Avenue, Portsmouth PO2, UK\",\n     \"geometry\" : {\n        \"location\" : {\n           \"lat\" : 50.8170795,\n           \"lng\" : -1.0709701\n        },\n        \"location_type\" : \"ROOFTOP\",\n        \"viewport\" : {\n           \"northeast\" : {\n              \"lat\" : 50.81842848029149,\n              \"lng\" : -1.069621119708498\n           },\n           \"southwest\" : {\n              \"lat\" : 50.8157305197085,\n              \"lng\" : -1.072319080291502\n           }\n        }\n     },\n     \"types\" : [ \"street_address\" ]\n  }\n   ],\n\"status\" : \"OK\"\n}\n<\/code><\/pre>\n<p>also heres a link to my dev page with my current code in full<\/p>\n<p>In summary, how do I get &#8220;Stubbington Avenue&#8221; from that mess up there ?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How can I parse through the response from a reverse geocode using Google Maps JavaScript API v3. geocoder.geocode({&#8216;latLng&#8217;: latlng}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[0]) { infowindow.setContent(results[0].formatted_address); infowindow.open(map, marker); } } This displays the formatted address fine in the popup, but I&#8217;m trying to take other bits out of the response, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7069","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7069"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7069\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}