日期:2014-05-16 浏览次数:20514 次
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(); } }
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; @Override public 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<S