Page 1 of 1
Trouble w json_decode
Posted: Sun Nov 28, 2010 2:15 am
by evreeland
I am trying to parse a json file I'm getting via http request from the Google Geocoding API
http://code.google.com/apis/maps/docume ... geocoding/
Here is my php but for some reason I am having trouble accessing the array.
For some reason the var_dump function is returning one long string instead of the hierarchical list of arrays. Any suggestions?
Code: Select all
function get_address_query($vendorid)
{
$vendor_address = $this->Vendor_model->get_address_query($vendorid);
$httprequest = "http://maps.googleapis.com/maps/api/geocode/json?address=".$vendor_address."&sensor=false";
$jsonlocation = file_get_contents($httprequest);
var_dump(json_decode($jsonlocation, true));
}
Re: Trouble w json_decode
Posted: Sun Nov 28, 2010 6:18 am
by requinix
What's the value of $jsonlocation?
Re: Trouble w json_decode
Posted: Sun Nov 28, 2010 12:11 pm
by evreeland
It's one long string of the resulting json file from the Google Maps API
{ "status": "OK", "results": [ { "types": [ "street_address" ], "formatted_address": "13209 Dobbins Pl, Los Angeles, CA 90049, USA", "address_components": [ { "long_name": "13209", "short_name": "13209", "types": [ "street_number" ] }, { "long_name": "Dobbins Pl", "short_name": "Dobbins Pl", "types": [ "route" ] }, { "long_name": "Los Angeles", "short_name": "Los Angeles", "types": [ "locality", "political" ] }, { "long_name": "Los Angeles", "short_name": "Los Angeles", "types": [ "administrative_area_level_3", "political" ] }, { "long_name": "Los Angeles", "short_name": "Los Angeles", "types": [ "administrative_area_level_2", "political" ] }, { "long_name": "California", "short_name": "CA", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "United States", "short_name": "US", "types": [ "country", "political" ] }, { "long_name": "90049", "short_name": "90049", "types": [ "postal_code" ] } ], "geometry": { "location": { "lat": 34.0531120, "lng": -118.4978380 }, "location_type": "ROOFTOP", "viewport": { "southwest": { "lat": 34.0499644, "lng": -118.5009856 }, "northeast": { "lat": 34.0562596, "lng": -118.4946904 } } } } ] }
Re: Trouble w json_decode
Posted: Sun Nov 28, 2010 12:56 pm
by Darhazer
What is the result of var_dump? Does it begins with array(x) or with string(x) ?
What is your PHP error?
Re: Trouble w json_decode
Posted: Sun Nov 28, 2010 5:57 pm
by evreeland
This is the vardump
The php error is Message: Undefined index: types
array(2) { ["status"]=> string(2) "OK" ["results"]=> array(1) { [0]=> array(4) { ["types"]=> array(1) { [0]=> string(14) "street_address" } ["formatted_address"]=> string(44) "13209 Dobbins Pl, Los Angeles, CA 90049, USA" ["address_components"]=> array(8) { [0]=> array(3) { ["long_name"]=> string(5) "13209" ["short_name"]=> string(5) "13209" ["types"]=> array(1) { [0]=> string(13) "street_number" } } [1]=> array(3) { ["long_name"]=> string(10) "Dobbins Pl" ["short_name"]=> string(10) "Dobbins Pl" ["types"]=> array(1) { [0]=> string(5) "route" } } [2]=> array(3) { ["long_name"]=> string(11) "Los Angeles" ["short_name"]=> string(11) "Los Angeles" ["types"]=> array(2) { [0]=> string(8) "locality" [1]=> string(9) "political" } } [3]=> array(3) { ["long_name"]=> string(11) "Los Angeles" ["short_name"]=> string(11) "Los Angeles" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_3" [1]=> string(9) "political" } } [4]=> array(3) { ["long_name"]=> string(11) "Los Angeles" ["short_name"]=> string(11) "Los Angeles" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_2" [1]=> string(9) "political" } } [5]=> array(3) { ["long_name"]=> string(10) "California" ["short_name"]=> string(2) "CA" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_1" [1]=> string(9) "political" } } [6]=> array(3) { ["long_name"]=> string(13) "United States" ["short_name"]=> string(2) "US" ["types"]=> array(2) { [0]=> string(7) "country" [1]=> string(9) "political" } } [7]=> array(3) { ["long_name"]=> string(5) "90049" ["short_name"]=> string(5) "90049" ["types"]=> array(1) { [0]=> string(11) "postal_code" } } } ["geometry"]=> array(3) { ["location"]=> array(2) { ["lat"]=> float(34.053112) ["lng"]=> float(-118.497838) } ["location_type"]=> string(7) "ROOFTOP" ["viewport"]=> array(2) { ["southwest"]=> array(2) { ["lat"]=> float(34.0499644) ["lng"]=> float(-118.5009856) } ["northeast"]=> array(2) { ["lat"]=> float(34.0562596) ["lng"]=> float(-118.4946904) } } } } } }
Re: Trouble w json_decode
Posted: Sun Nov 28, 2010 6:40 pm
by Jonah Bron
That's not a string, it looks like it's being parsed successfully. I see the index "types" in there, too.
Re: Trouble w json_decode
Posted: Mon Nov 29, 2010 6:38 am
by Darhazer
The problem is not with json, but with your code, so please post it

by the way, if you add <pre> tag before your output, you will see the 'string' in more beautiful way
