2011-07-05 85 views
0

我從Google GeoCode獲得了這個JSON,我需要解碼這個,然後獲取administrative_area_level_2和postal_code,然後完成。我可以解析結果,但之後我不確定如何獲得結果的重置。我試圖格式化,但它不工作。如果您按照鏈接,這是格式化版本。解析Google Geocode JSON

我想這樣做是JAVA,我知道我可以在PHP中這樣做,但我寧願在JAVA中這樣做。預先感謝您的幫助。

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false

{ 
    "results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "279-281", 
       "short_name" : "279-281", 
       "types" : [ "street_number" ] 
      }, 
      { 
       "long_name" : "Bedford Ave", 
       "short_name" : "Bedford Ave", 
       "types" : [ "route" ] 
      }, 
      { 
       "long_name" : "Williamsburg", 
       "short_name" : "Williamsburg", 
       "types" : [ "neighborhood", "political" ] 
      }, 
      { 
       "long_name" : "Brooklyn", 
       "short_name" : "Brooklyn", 
       "types" : [ "sublocality", "political" ] 
      }, 
      { 
       "long_name" : "Kings", 
       "short_name" : "Kings", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "New York", 
       "short_name" : "NY", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      }, 
      { 
       "long_name" : "11211", 
       "short_name" : "11211", 
       "types" : [ "postal_code" ] 
      } 
     ], 
     "formatted_address" : "279-281 Bedford Ave, Brooklyn, NY 11211, USA", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 40.71450470, 
        "lng" : -73.96128740 
       }, 
       "southwest" : { 
        "lat" : 40.7139010, 
        "lng" : -73.961680 
       } 
      }, 
      "location" : { 
       "lat" : 40.71422150, 
       "lng" : -73.96144540 
      }, 
      "location_type" : "RANGE_INTERPOLATED", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 40.71735047068017, 
        "lng" : -73.95833607931984 
       }, 
       "southwest" : { 
        "lat" : 40.71105522931983, 
        "lng" : -73.96463132068017 
       } 
      } 
     }, 
     "types" : [ "street_address" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "Williamsburg", 
       "short_name" : "Williamsburg", 
       "types" : [ "neighborhood", "political" ] 
      }, 
      { 
       "long_name" : "New York", 
       "short_name" : "New York", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "Kings", 
       "short_name" : "Kings", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "New York", 
       "short_name" : "NY", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "Williamsburg, NY, USA", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 40.7182050, 
        "lng" : -73.9202810 
       }, 
       "southwest" : { 
        "lat" : 40.6979330, 
        "lng" : -73.96984510 
       } 
      }, 
      "location" : { 
       "lat" : 40.70644610, 
       "lng" : -73.95361629999999 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 40.7182050, 
        "lng" : -73.9202810 
       }, 
       "southwest" : { 
        "lat" : 40.6979330, 
        "lng" : -73.96984510 
       } 
      } 
     }, 
     "types" : [ "neighborhood", "political" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "11211", 
       "short_name" : "11211", 
       "types" : [ "postal_code" ] 
      }, 
      { 
       "long_name" : "Brooklyn", 
       "short_name" : "Brooklyn", 
       "types" : [ "sublocality", "political" ] 
      }, 
      { 
       "long_name" : "New York", 
       "short_name" : "New York", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "New York", 
       "short_name" : "NY", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "Brooklyn, NY 11211, USA", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 40.7261370, 
        "lng" : -73.92304290 
       }, 
       "southwest" : { 
        "lat" : 40.69763590, 
        "lng" : -73.97616690 
       } 
      }, 
      "location" : { 
       "lat" : 40.71800360, 
       "lng" : -73.96537150000002 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 40.7261370, 
        "lng" : -73.92304290 
       }, 
       "southwest" : { 
        "lat" : 40.69763590, 
        "lng" : -73.97616690 
       } 
      } 
     }, 
     "types" : [ "postal_code" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "Brooklyn", 
       "short_name" : "Brooklyn", 
       "types" : [ "sublocality", "political" ] 
      }, 
      { 
       "long_name" : "New York", 
       "short_name" : "New York", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "Kings", 
       "short_name" : "Kings", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "New York", 
       "short_name" : "NY", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "Brooklyn, NY, USA", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 40.7394460, 
        "lng" : -73.8333650 
       }, 
       "southwest" : { 
        "lat" : 40.55104190, 
        "lng" : -74.056630 
       } 
      }, 
      "location" : { 
       "lat" : 40.650, 
       "lng" : -73.950 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 40.7394460, 
        "lng" : -73.8333650 
       }, 
       "southwest" : { 
        "lat" : 40.55104190, 
        "lng" : -74.056630 
       } 
      } 
     }, 
     "types" : [ "sublocality", "political" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "Kings", 
       "short_name" : "Kings", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "New York", 
       "short_name" : "NY", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "Kings, New York, USA", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 40.7394460, 
        "lng" : -73.8333650 
       }, 
       "southwest" : { 
        "lat" : 40.55104190, 
        "lng" : -74.056630 
       } 
      }, 
      "location" : { 
       "lat" : 40.65287620, 
       "lng" : -73.95949399999999 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 40.7394460, 
        "lng" : -73.8333650 
       }, 
       "southwest" : { 
        "lat" : 40.55104190, 
        "lng" : -74.056630 
       } 
      } 
     }, 
     "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "New York", 
       "short_name" : "New York", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "New York", 
       "short_name" : "New York", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "New York", 
       "short_name" : "NY", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "New York, NY, USA", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 40.9175770, 
        "lng" : -73.7002720 
       }, 
       "southwest" : { 
        "lat" : 40.4773990, 
        "lng" : -74.259090 
       } 
      }, 
      "location" : { 
       "lat" : 40.71435280, 
       "lng" : -74.00597309999999 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 40.9175770, 
        "lng" : -73.7002720 
       }, 
       "southwest" : { 
        "lat" : 40.4773990, 
        "lng" : -74.259090 
       } 
      } 
     }, 
     "types" : [ "locality", "political" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "New York", 
       "short_name" : "NY", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "New York, USA", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 45.0158650, 
        "lng" : -71.7774910 
       }, 
       "southwest" : { 
        "lat" : 40.4773990, 
        "lng" : -79.762590 
       } 
      }, 
      "location" : { 
       "lat" : 43.29942850, 
       "lng" : -74.21793260000001 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 45.0158650, 
        "lng" : -71.7774910 
       }, 
       "southwest" : { 
        "lat" : 40.4773990, 
        "lng" : -79.762590 
       } 
      } 
     }, 
     "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "United States", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 71.53879999999999, 
        "lng" : -66.88507489999999 
       }, 
       "southwest" : { 
        "lat" : 18.77630, 
        "lng" : 170.59570 
       } 
      }, 
      "location" : { 
       "lat" : 37.090240, 
       "lng" : -95.7128910 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 71.53879999999999, 
        "lng" : -66.88507489999999 
       }, 
       "southwest" : { 
        "lat" : 18.77630, 
        "lng" : 170.59570 
       } 
      } 
     }, 
     "types" : [ "country", "political" ] 
     } 
    ], 
    "status" : "OK" 
} 
+1

你有沒有試過*使用JSON解析庫? –

+0

請參閱http://stackoverflow.com/questions/1688099/converting-json-to-java/1688182#1688182關於在java中解析json的提示。 – Matten

+0

我可以毫無問題地解析它,並以字符串形式獲得上述結果。我在提取郵政編碼和管理員級別2時遇到問題。這是我沒有得到的部分。 –

回答

-4

我設法弄明白。我編寫了一些代碼來遍歷每個對象,直到我從String中獲得所需的東西。我將在今天晚些時候發佈代碼。感謝您的幫助。它只是很多數據,我想也許有人會有一個更簡單的方法。

+1

代碼在哪裏? –

+0

有沒有代碼需要傢伙。他解釋說他的方式和他的方式。這是一個暗示,可以幫助人們解決這個問題,所以不要低估它! – WoIIe