简体   繁体   中英

How retrieve “locality” from json google places api

How retrieve "locality" from json google places api

in this json response:

{
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "1623",
            "short_name" : "1623",
            "types" : [ "street_number" ]
         },
     {
        "long_name" : "1/2 N Cahuenga Boulevard",
        "short_name" : "1/2 N Cahuenga Boulevard",
        "types" : [ "route" ]
         },
         {
            "long_name" : "Los Angeles",
            "short_name" : "Los Angeles",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "CA",
            "short_name" : "CA",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "US",
            "short_name" : "US",
            "types" : [ "country", "political" ]
         },
     {
        "long_name" : "90028",
        "short_name" : "90028",
        "types" : [ "postal_code" ]
     }

i am using this code to get "lat" and "lng" :

            JSONObject jsonLocation = c.getJSONObject("geometry").getJSONObject("location");

            places_latitude = jsonLocation.getString("lat");
            places_longitude = jsonLocation.getString("lng");

how do i get the "locality" value ? i'am using java.

Thank you,

Carlos.

I follow your way.

JSONArray address_components = c.getJSONObject("result").getJSONArray("address_components");

JSONObject obj = address_components.getJSONObject(2)
/*
 obj is 
  {
            "long_name" : "Los Angeles",
            "short_name" : "Los Angeles",
            "types" : [ "locality", "political" ]
   }
 */
String your_result = obj.getJSONArray("types").get(0).toString();

The original json should be

{
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "1623",
            "short_name" : "1623",
            "types" : [ "street_number" ]
         },
     {
        "long_name" : "1/2 N Cahuenga Boulevard",
        "short_name" : "1/2 N Cahuenga Boulevard",
        "types" : [ "route" ]
         },
         {
            "long_name" : "Los Angeles",
            "short_name" : "Los Angeles",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "CA",
            "short_name" : "CA",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "US",
            "short_name" : "US",
            "types" : [ "country", "political" ]
         },
     {
        "long_name" : "90028",
        "short_name" : "90028",
        "types" : [ "postal_code" ]
     }
    ]
   }
}

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