获取JSON对象的一部分


Get part of JSON object

我正在玩谷歌地图和地理编码(将地址转换为lang/lat坐标(。到目前为止一切都很好,我从谷歌地图服务器上得到了一个文件的答案:

$address = urlencode( $address );
$url = "http://maps.google.com/maps/api/geocode/json?address={$address}&sensor=false";
$content = file_get_contents( $url );
echo '<pre>';
    print_r( $content );
echo '</pre>';

重点是我不需要所有的信息。

问题

A如何只获取JSON对象的一部分?(示例:我只需要'results'->'address_components'->'type' = 'country';。(

B我可以以某种方式减少返回(谷歌(文件的内容吗,因为我不需要所有东西?

更新

我需要将部分数据添加到json对象中。所以解码&那么编码看起来像是能量的损失&时间(此时(。如果我请求XML对象会更好(更快(,有什么想法吗?(最后一组对象相当大。这就是为什么我应该真正关心性能的原因。(


来自谷歌地图服务器的示例JSON答案(文件内容(:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "28",
               "short_name" : "28",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Sellhorner Weg",
               "short_name" : "Sellhorner Weg",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Behringen",
               "short_name" : "Behringen",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Bispingen",
               "short_name" : "Bispingen",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Soltau-Fallingbostel",
               "short_name" : "Soltau-Fallingbostel",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Niedersachsen",
               "short_name" : "NDS",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Deutschland",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "29646",
               "short_name" : "29646",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Sellhorner Weg 28, 29646 Bispingen, Deutschland",
         "geometry" : {
            "location" : {
               "lat" : 53.118080,
               "lng" : 9.966859999999999
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 53.11942898029150,
                  "lng" : 9.96820898029150
               },
               "southwest" : {
                  "lat" : 53.11673101970850,
                  "lng" : 9.965511019708496
               }
            }
         },
         "partial_match" : true,
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}
$array = json_decode($content, true);
$countries = array_filter($array['results']['address_components'], function ($a) {
    return in_array('country', $a['types']);
});
var_dump($countries);

为您提供所有类型为'country'address_components。也许这只是一个,但它可能不止一个。此语法需要PHP 5.3+BTW.

我可以以某种方式减少返回(谷歌(文件的内容吗,因为我不需要所有东西?

只要把解码后的$array中你需要的部分拿走,剩下的就忘了。如果你愿意,再把它json_encode一遍。