简体   繁体   中英

How can i get the value from json

Here is my request for the getting the reverse geocode

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

Here is my JSON result

{
   "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"
}

How can I get the value inside the formatted_address

ie, The value Mettupalayam Road, Bilichi, Tamil Nadu 641104, India

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

use php's json_decode

$jsondata = json_decode ( $result, true);

after that you can use

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

PS: in json_decode, the first is the string to be converted to json and second is option whether to return object (false) or just associative arrays (true)

Try this..

  $json = json_decode ( $result, true);

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

result: Mettupalayam Road, Bilichi, Tamil Nadu 641104, India

u can try this way:

$json = 'your json here';

$data = json_decode($json);

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

result: Mettupalayam Road, Bilichi, Tamil Nadu 641104, India

u can change index 0 to 1,2,3 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM