2011-08-27 75 views
2

我通常喜歡以下單級JSON玩...如何獲得JSON子項

<?php 
$json = '{"foo-bar": 12345}'; 
$obj = json_decode($json); 
print $obj->{'foo-bar'}; // 12345 
?> 

非常基本的,我怎麼摘掉孩子的物品如在此谷歌地圖JSON。例如,latitiude

{ 
    "results" : [ 
    { 
    "address_components" : [ 
     { 
      "long_name" : "1600", 
      "short_name" : "1600", 
      "types" : [ "street_number" ] 
     }, 
     { 
      "long_name" : "Amphitheatre Pkwy", 
      "short_name" : "Amphitheatre Pkwy", 
      "types" : [ "route" ] 
     }, 
     { 
      "long_name" : "Mountain View", 
      "short_name" : "Mountain View", 
      "types" : [ "locality", "political" ] 
     }, 
     { 
      "long_name" : "San Jose", 
      "short_name" : "San Jose", 
      "types" : [ "administrative_area_level_3", "political" ] 
     }, 
     { 
      "long_name" : "Santa Clara", 
      "short_name" : "Santa Clara", 
      "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" : "94043", 
      "short_name" : "94043", 
      "types" : [ "postal_code" ] 
     } 
    ], 
    "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", 
    "geometry" : { 
     "location" : { 
      "lat" : 37.4220110, 
      "lng" : -122.08406610 
     }, 
     "location_type" : "ROOFTOP", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 37.42335998029150, 
       "lng" : -122.0827171197085 
      }, 
      "southwest" : { 
       "lat" : 37.42066201970850, 
       "lng" : -122.0854150802915 
      } 
     } 
    }, 
    "types" : [ "street_address" ] 
    } 
    ], 
    "status" : "OK" 
} 
+0

可能的重複[使用PHP從JSON讀取值](http://stackoverflow.com/questions/2452079/read-a-value-from-json-using-php) –

回答

0

簡單

$json = json_decode($json); 
echo $json->results[0]->geometry->location->lat; 
0

你可能想看看Read a value from JSON using PHP,@Ignacio巴斯克斯 - 艾布拉姆斯說

在PHP中,解碼JSON對象的對象和數組將數組:

$data->results[0]->geometry->location->lat 
    $data->results[0]->geometry->location->lng