Trouble w json_decode

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
evreeland
Forum Newbie
Posts: 3
Joined: Sun Nov 28, 2010 2:07 am

Trouble w json_decode

Post 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));     
    }
    
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Trouble w json_decode

Post by requinix »

What's the value of $jsonlocation?
evreeland
Forum Newbie
Posts: 3
Joined: Sun Nov 28, 2010 2:07 am

Re: Trouble w json_decode

Post 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 } } } } ] }
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Trouble w json_decode

Post by Darhazer »

What is the result of var_dump? Does it begins with array(x) or with string(x) ?
What is your PHP error?
evreeland
Forum Newbie
Posts: 3
Joined: Sun Nov 28, 2010 2:07 am

Re: Trouble w json_decode

Post 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) } } } } } }
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Trouble w json_decode

Post by Jonah Bron »

That's not a string, it looks like it's being parsed successfully. I see the index "types" in there, too.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Trouble w json_decode

Post 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 :)
Post Reply