2013-11-24 140 views
0

我使用openstreetmap來獲取當前城市名稱。我發送的網址是:解析Android中的JSON,如何解析

http://nominatim.openstreetmap.org/reverse?format=json&lat=52.5487429714954&lon=-1.81602098644987&zoom=18&addressdetails=1

我得到這個結果作爲JSON:

{"place_id":"62762024","licence":"Data \u00a9 OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright","osm_type":"way","osm_id":"90394420","lat":"52.548781","lon":"-1.81626870827795","display_name":"137, Pilkington Avenue, Castle Vale, Birmingham, West Midlands, England, B72 1LH, United Kingdom","address":{"house_number":"137","road":"Pilkington Avenue","suburb":"Castle Vale","city":"Birmingham","county":"West Midlands","state_district":"West Midlands","state":"England","postcode":"B72 1LH","country":"United Kingdom","country_code":"gb"}} 

我如何使用它在我的代碼。

Button b = (Button) findViewById(R.id.button123); 
b.setOnClickListener(new OnClickListener(){ 
    public void onClick(View v) { 
     try { 
      String url="http://nominatim.openstreetmap.org/reverseformat=json&lat="+currentLatitude+"&lon="+currentLongitude+"&zoom=18&addressdetails=1"; 
      s = getJson(url); 
      if(s!=null){ 
       JSONObject jObj = new JSONObject(s); 
       String exResult = jObj.getJSONObject("SOMETHING")//I don't know what to put here 

回答

0

獲取值及其標籤。如:

String placeId = jObj.getString("place_id"); 
String licence = jObj.getString("license"); 

JSONObject addressJsonObject = jObj.getJSONObject("address"); 
String houseNumber = addressJsonObject.getString("house_number"); 

編輯: BTW這個頁面可以幫助你理解任何JSON字符串的結構。在那裏複製並粘貼你的json字符串。 http://json.parser.online.fr/

+0

我試過的代碼,但該地址是NULL –

+0

也許你正在從服務中不同的反應。檢查是否將json響應(您的案例中的s值)複製到我共享的地址。你看到地址的任何價值嗎? – Devrim

0

喜歡的東西:

String exResult = jObj.getJSONObject("address").getString("city");