0

如何从 json google places api 中检索“locality”

在这个 json 响应中:

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

我正在使用此代码来获取“lat”和“lng”:

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

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

我如何获得“位置”值?我正在使用java。

谢谢,

卡洛斯。

4

1 回答 1

1

我按照你的方式。

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();

原始的json应该是

{
   "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" ]
     }
    ]
   }
}
于 2012-08-09T04:10:12.553 回答