JSON解析分為以下幾個步驟
1.下載url上的所有的json資料
package mars.com;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;public class HttpDownloader {// 根據URL下載檔案,前提是這個檔案當中的內容是文本,函數的傳回值就是文本當中的內容// 1.建立一個URL對象// 2.通過URL對象,建立一個HttpURLConnection對象// 3.得到InputStream// 4.從InputStream當中讀取資料private URL url;public String download(String urlStr) {StringBuffer sb = new StringBuffer();String line = null;BufferedReader buffer = null;try {url = new URL(urlStr);HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));while ((line = buffer.readLine()) != null) {sb.append(line);}} catch (Exception e) {e.printStackTrace();} finally {try {buffer.close();} catch (IOException e) {e.printStackTrace();}}return sb.toString();}}
2.
package mars.com;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.json.JSONArray;import org.json.JSONObject;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class DemoMapJSONActivity extends Activity {private Button button;private TextView show;private EditText longitude;private EditText latitude;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button = (Button) findViewById(R.id.location);show = (TextView) findViewById(R.id.show);longitude = (EditText) findViewById(R.id.longitude);latitude = (EditText) findViewById(R.id.latitude);button.setOnClickListener(new MyLocationListener());}class MyLocationListener implements OnClickListener {public void onClick(View v) {HttpDownloader HTT = new HttpDownloader();String strUrl = "http://maps.googleapis.com/maps/api/geocode/json?latlng="+ latitude.getText().toString()+ ","+ longitude.getText().toString()+ "&sensor=false&language=zh-CN";String jsonData = HTT.download(strUrl);StringBuffer buf = new StringBuffer();try {Map<String, Object> result = parseJson(jsonData);List<Map<String, Object>> all = (List<Map<String, Object>>) result.get("results");Iterator<Map<String, Object>> iterator = all.iterator();int i = 0;while (iterator.hasNext()) {Map<String, Object> map = iterator.next();buf.append("\n地址:" + map.get("formatted_address")+ "*********" + i++);break;}Map<String, Object> address_components = parseJson(jsonData);List<Map<String, Object>> all2 = (List<Map<String, Object>>) address_components.get("address_components");Iterator<Map<String, Object>> iterator2 = all2.iterator();int j = 0;while (iterator2.hasNext()) {Map<String, Object> map = iterator2.next();buf.append("\n地址:" + map.get("long_name") + "*********"+ j++);}} catch (Exception e) {e.printStackTrace();}show.setText(buf);}}private Map<String, Object> parseJson(String data) throws Exception {Map<String, Object> allMap = new HashMap<String, Object>();JSONObject allData = new JSONObject(data);// 全部的內容變為一個項JSONArray jsonArray = allData.getJSONArray("results");// 取出數組List<Map<String, Object>> all = new ArrayList<Map<String, Object>>();List<Map<String, Object>> all2 = new ArrayList<Map<String, Object>>();for (int i = 0; i < jsonArray.length(); i++) {Map<String, Object> map = new HashMap<String, Object>();JSONObject jsonObject = jsonArray.getJSONObject(i);map.put("formatted_address",jsonObject.getString("formatted_address"));all.add(map);if (i != 0) {continue;}// 只取出第一組完整資料,其他的都沒這組詳細JSONArray jsonArray2 = jsonObject.getJSONArray("address_components");for (int j = 0; j < jsonArray2.length(); j++) {Map<String, Object> map2 = new HashMap<String, Object>();JSONObject jsonObject3 = jsonArray2.getJSONObject(j);map2.put("long_name", jsonObject3.get("long_name"));all2.add(map2);System.out.println("****");}}allMap.put("results", all);// 所有的取出的簡直對的list,放到results底下了allMap.put("address_components", all2);return allMap;}}
3.main.xml檔案
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/longitude" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="114.3579530" /> <EditText android:id="@+id/latitude" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="30.6096780" /> <Button android:id="@+id/location" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="定位" /> <TextView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="顯示" /></LinearLayout>
4.配置許可權
<uses-permission android:name="android.permission.INTERNET" />
怕有些人看不懂代碼,在此說明以下,我這裡只是解析的我需要的最詳細地址,其實這個json已經足夠複雜了,但是我實際上是經過偷工減料的,我只取了最詳細的一組資料
以及最詳細的分組資料。所以沒有用到工具類。
本來應該用工具類來著,比如說裡面的results應該單獨是一個類才行,但是我沒有用他的其他的屬性,所以沒有詳細的解析。
如果想使用它的其他的屬性,比如說address_components,formatted_address
,geometry,types。那你得詳細的把這個類的結構寫出來,然後以索引值對的形式放到List中,然後你想取哪個資料都可以了,就看你了。
json資料如下
{ "results" : [ { "address_components" : [ { "long_name" : "1178號", "short_name" : "1178號", "types" : [ "street_number" ] }, { "long_name" : "和平大道", "short_name" : "和平大道", "types" : [ "route" ] }, { "long_name" : "武昌區", "short_name" : "武昌區", "types" : [ "sublocality", "political" ] }, { "long_name" : "武漢", "short_name" : "武漢", "types" : [ "locality", "political" ] }, { "long_name" : "湖北省", "short_name" : "湖北省", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "中國", "short_name" : "CN", "types" : [ "country", "political" ] } ], "formatted_address" : "中國湖北省武漢市武昌區和平大道1178號", "geometry" : { "location" : { "lat" : 30.61071999999999, "lng" : 114.3566410 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 30.61206898029149, "lng" : 114.3579899802915 }, "southwest" : { "lat" : 30.60937101970850, "lng" : 114.3552920197085 } } }, "types" : [ "street_address" ] }, { "address_components" : [ { "long_name" : "武昌區", "short_name" : "武昌區", "types" : [ "sublocality", "political" ] }, { "long_name" : "武漢", "short_name" : "武漢", "types" : [ "locality", "political" ] }, { "long_name" : "湖北省", "short_name" : "湖北省", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "中國", "short_name" : "CN", "types" : [ "country", "political" ] } ], "formatted_address" : "中國湖北省武漢市武昌區", "geometry" : { "bounds" : { "northeast" : { "lat" : 30.62807880, "lng" : 114.42031060 }, "southwest" : { "lat" : 30.49578920, "lng" : 114.26042410 } }, "location" : { "lat" : 30.5538990, "lng" : 114.3158970 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 30.62807880, "lng" : 114.42031060 }, "southwest" : { "lat" : 30.49578920, "lng" : 114.26042410 } } }, "types" : [ "sublocality", "political" ] }, { "address_components" : [ { "long_name" : "武漢", "short_name" : "武漢", "types" : [ "locality", "political" ] }, { "long_name" : "湖北省", "short_name" : "湖北省", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "中國", "short_name" : "CN", "types" : [ "country", "political" ] } ], "formatted_address" : "中國湖北省武漢市", "geometry" : { "bounds" : { "northeast" : { "lat" : 31.36126030, "lng" : 115.08257280 }, "southwest" : { "lat" : 29.96907670, "lng" : 113.70228110 } }, "location" : { "lat" : 30.5930870, "lng" : 114.3053570 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 30.78745989999999, "lng" : 114.6189880 }, "southwest" : { "lat" : 30.34877210, "lng" : 113.9817810 } } }, "types" : [ "locality", "political" ] }, { "address_components" : [ { "long_name" : "湖北省", "short_name" : "湖北省", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "中國", "short_name" : "CN", "types" : [ "country", "political" ] } ], "formatted_address" : "中國湖北省", "geometry" : { "bounds" : { "northeast" : { "lat" : 33.27561610, "lng" : 116.13484340 }, "southwest" : { "lat" : 29.02948840, "lng" : 108.36696460 } }, "location" : { "lat" : 30.5458610, "lng" : 114.3419210 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 33.27561610, "lng" : 116.13484340 }, "southwest" : { "lat" : 29.02948840, "lng" : 108.36696460 } } }, "types" : [ "administrative_area_level_1", "political" ] }, { "address_components" : [ { "long_name" : "中國", "short_name" : "CN", "types" : [ "country", "political" ] } ], "formatted_address" : "中國", "geometry" : { "bounds" : { "northeast" : { "lat" : 53.56097399999999, "lng" : 134.772810 }, "southwest" : { "lat" : 18.15352160, "lng" : 73.49941369999999 } }, "location" : { "lat" : 35.861660, "lng" : 104.1953970 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 53.56097399999999, "lng" : 134.772810 }, "southwest" : { "lat" : 18.15352160, "lng" : 73.49941369999999 } } }, "types" : [ "country", "political" ] } ], "status" : "OK"}