2012-08-05 63 views
0

如何檢索該JSON響應從JSON谷歌Places API的如何從JSON谷歌Places API的檢索 「局部性」

「局部性」:

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

謝謝,

卡洛斯。

+1

的問題是,有沒有地方的價值。 您需要做的是將Google爲您提供的部分迭代爲address_components,查看它們的類型,並在找到合適的類型時停止。然後獲取該對象的long_name。 – 2012-08-05 13:34:22

+0

謝謝。解決了。 – 2012-08-05 14:37:58

回答

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