解析Android中的JSON,如何解析

问题描述:

我使用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 

获取值及其标签。如:

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

喜欢的东西:

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