我如何从json获得价值

时间:2015-01-31 10:00:41

标签: php arrays json google-maps

以下是我获取反向地理编码的请求

https://maps.googleapis.com/maps/api/geocode/json?latlng=11.2100,76.9500&key=AIzaSyC2pESdfUbnL2i1eHrJY4v6cWLI9EJePCo

这是我的JSON结果

{
   "results":[
      {
         "address_components":[
            {
               "long_name":"Mettupalayam Road",
               "short_name":"NH67",
               "types":[
                  "route"
               ]
            },
            {
               "long_name":"Bilichi",
               "short_name":"Bilichi",
               "types":[
                  "locality",
                  "political"
               ]
            },
            {
               "long_name":"Coimbatore",
               "short_name":"Coimbatore",
               "types":[
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name":"Tamil Nadu",
               "short_name":"TN",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"India",
               "short_name":"IN",
               "types":[
                  "country",
                  "political"
               ]
            },
            {
               "long_name":"641104",
               "short_name":"641104",
               "types":[
                  "postal_code"
               ]
            }
         ],
         "formatted_address":"Mettupalayam Road, Bilichi, Tamil Nadu 641104, India",
         "geometry":{
            "bounds":{
               "northeast":{
                  "lat":11.2179034,
                  "lng":76.96615869999999
               },
               "southwest":{
                  "lat":11.211082,
                  "lng":76.96400589999999
               }
            },
            "location":{
               "lat":11.2145941,
               "lng":76.9653811
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":11.2179034,
                  "lng":76.96643128029149
               },
               "southwest":{
                  "lat":11.211082,
                  "lng":76.96373331970848
               }
            }
         },
         "types":[
            "route"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"641104",
               "short_name":"641104",
               "types":[
                  "postal_code"
               ]
            },
            {
               "long_name":"Coimbatore",
               "short_name":"Coimbatore",
               "types":[
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name":"Tamil Nadu",
               "short_name":"TN",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"India",
               "short_name":"IN",
               "types":[
                  "country",
                  "political"
               ]
            }
         ],
         "formatted_address":"Tamil Nadu 641104, India",
         "geometry":{
            "bounds":{
               "northeast":{
                  "lat":11.2855508,
                  "lng":77.00267029999999
               },
               "southwest":{
                  "lat":11.1837084,
                  "lng":76.8981429
               }
            },
            "location":{
               "lat":11.2460456,
               "lng":76.9527836
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":11.2855508,
                  "lng":77.00267029999999
               },
               "southwest":{
                  "lat":11.1837084,
                  "lng":76.8981429
               }
            }
         },
         "types":[
            "postal_code"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"Coimbatore",
               "short_name":"Coimbatore",
               "types":[
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name":"Tamil Nadu",
               "short_name":"TN",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"India",
               "short_name":"IN",
               "types":[
                  "country",
                  "political"
               ]
            }
         ],
         "formatted_address":"Coimbatore, Tamil Nadu, India",
         "geometry":{
            "bounds":{
               "northeast":{
                  "lat":11.4057969,
                  "lng":77.1766741
               },
               "southwest":{
                  "lat":10.21881,
                  "lng":76.656036
               }
            },
            "location":{
               "lat":10.9675289,
               "lng":76.91820969999999
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":11.4057969,
                  "lng":77.1766741
               },
               "southwest":{
                  "lat":10.21881,
                  "lng":76.656036
               }
            }
         },
         "types":[
            "administrative_area_level_2",
            "political"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"Tamil Nadu",
               "short_name":"TN",
               "types":[
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name":"India",
               "short_name":"IN",
               "types":[
                  "country",
                  "political"
               ]
            }
         ],
         "formatted_address":"Tamil Nadu, India",
         "geometry":{
            "bounds":{
               "northeast":{
                  "lat":13.496666,
                  "lng":80.3464511
               },
               "southwest":{
                  "lat":8.0774291,
                  "lng":76.23055409999999
               }
            },
            "location":{
               "lat":11.1271225,
               "lng":78.6568942
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":13.496666,
                  "lng":80.3464511
               },
               "southwest":{
                  "lat":8.0774291,
                  "lng":76.23055409999999
               }
            }
         },
         "types":[
            "administrative_area_level_1",
            "political"
         ]
      },
      {
         "address_components":[
            {
               "long_name":"India",
               "short_name":"IN",
               "types":[
                  "country",
                  "political"
               ]
            }
         ],
         "formatted_address":"India",
         "geometry":{
            "bounds":{
               "northeast":{
                  "lat":35.5044752,
                  "lng":97.395555
               },
               "southwest":{
                  "lat":6.7535159,
                  "lng":68.162386
               }
            },
            "location":{
               "lat":20.593684,
               "lng":78.96288
            },
            "location_type":"APPROXIMATE",
            "viewport":{
               "northeast":{
                  "lat":35.5044752,
                  "lng":97.395555
               },
               "southwest":{
                  "lat":6.7535159,
                  "lng":68.16288519999999
               }
            }
         },
         "types":[
            "country",
            "political"
         ]
      }
   ],
   "status":"OK"
}

如何在formatted_address

中获取值

即,值Mettupalayam Road, Bilichi, Tamil Nadu 641104, India

"formatted_address" : "Mettupalayam Road, Bilichi, Tamil Nadu 641104, India",

3 个答案:

答案 0 :(得分:1)

使用php的json_decode

$jsondata = json_decode ( $result, true);

之后你可以使用

echo $jsondata['results']['formatted_address'];

PS:在json_decode中,第一个是要转换为json的字符串,第二个是选择是返回对象(false)还是只返回关联数组(true)

答案 1 :(得分:1)

试试这个..

  $json = json_decode ( $result, true);

    echo $json['results'][0]['formatted_address'];

结果: Mettupalayam Road,Bilichi,泰米尔纳德邦641104,印度

答案 2 :(得分:0)

你可以这样试试:

$json = 'your json here';

$ data = json_decode($ json);

echo $ data-> results [0] - > formatted_address;

结果:Mettupalayam Road,Bilichi,泰米尔纳德邦641104,印度

你可以将索引0改为1,2,3 ......