在android中解析JSON,如何解析

时间:2013-11-24 19:56:59

标签: android json

我使用openstreetmap获取当前城市名称。我发送的URL是:

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

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分:0)

类似的东西:

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