JSONActivity.java:
package com.cz.json; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.PrintStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.widget.TextView; public class JSONActivity extends Activity { private String nameData[] = new String[] { "小王", "小李", "小张" }; private int ageData[] = new int[] { 23, 25, 27 }; private boolean isMarraiedData[] = new boolean[] { false, true, false }; private double salaryData[] = new double[] { 7000.0, 5000.0, 3000.0 }; private Date birthdayData[] = new Date[] { new Date(), new Date(), new Date() }; private String companyName = "微软亚洲研究院"; private String companyAddr = "中国北京"; private String companyTel = "010-52354396"; private TextView msg = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); writeByJSON(); readByJSON(); } private void writeByJSON() { JSONObject jsonObject = new JSONObject(); // 建立最外面的节点对象 JSONArray jsonArray = new JSONArray(); // 定义数组 for (int x = 0; x < nameData.length; x++) { // 将数组内容配置到相应的节点 JSONObject temp = new JSONObject(); // 每一个包装的数据都是JSONObject try { temp.put("name", this.nameData[x]); temp.put("age", this.ageData[x]); temp.put("married", this.isMarraiedData[x]); temp.put("salary", this.salaryData[x]); temp.put("birthday", this.birthdayData[x]); } catch (JSONException e) { e.printStackTrace(); } jsonArray.put(temp); // 保存多个JSONObject } try { jsonObject.put("persondata", jsonArray); jsonObject.put("company", this.companyName); jsonObject.put("address", this.companyAddr); jsonObject.put("telephone", this.companyTel); } catch (JSONException e) { e.printStackTrace(); } if (!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // 不存在不操作 return; // 返回到程序的被调用处 } File file = new File(Environment.getExternalStorageDirectory() + File.separator + "xdwang" + File.separator + "json.txt"); // 要输出文件的路径 if (!file.getParentFile().exists()) { // 文件不存在 file.getParentFile().mkdirs(); // 创建文件夹 } PrintStream out = null; try { out = new PrintStream(new FileOutputStream(file)); out.print(jsonObject.toString()); // 将数据变为字符串后保存 } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (out != null) { out.close(); // 关闭输出 } } } private void readByJSON() { this.msg = (TextView) super.findViewById(R.id.msg); if (!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // 不存在不操作 return; // 返回到程序的被调用处 } File file = new File(Environment.getExternalStorageDirectory() + File.separator + "xdwang" + File.separator + "json.txt"); // 要输出文件的路径 if (!file.exists()) { // 文件不存在 return; } BufferedReader reader = null; String str = ""; try { reader = new BufferedReader(new FileReader(file)); String tempString = null; while ((tempString = reader.readLine()) != null) { str = str + tempString; } reader.close(); } catch (Exception e) { } finally { if (reader != null) { try { reader.close(); } catch (Exception e) { } } } StringBuffer buf = new StringBuffer(); try { Map<String, Object> result = this.parseJson(str); // 解析文本 buf.append("公司名称:" + result.get("company") + "\n"); buf.append("电话号码:" + result.get("telephone") + "\n"); buf.append("公司地址:" + result.get("address") + "\n"); List<Map<String, Object>> all = (List<Map<String, Object>>) result .get("persondata"); Iterator<Map<String, Object>> iter = all.iterator(); while (iter.hasNext()) { Map<String, Object> map = iter.next(); buf.append("姓名:" + map.get("name") + ",年龄:" + map.get("age") + "\n" + "工资:" + map.get("salary") + ",是否结婚:" + map.get("married") + "\n" + "生日:" + map.get("birthday") + "\n"); } } catch (Exception e) { e.printStackTrace(); } this.msg.setText(buf); } private Map<String, Object> parseJson(String data) throws Exception { Map<String, Object> maps = new HashMap<String, Object>(); JSONObject jsonObject = new JSONObject(data); // 全部的内容变为一个项 maps.put("company", jsonObject.getString("company")); // 取出项 maps.put("telephone", jsonObject.getString("telephone")); // 取出项 maps.put("address", jsonObject.getString("address")); // 取出项 JSONArray jsonArray = jsonObject.getJSONArray("persondata"); // 取出数组 List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>(); for (int x = 0; x < jsonArray.length(); x++) { Map<String, Object> map = new HashMap<String, Object>(); JSONObject jsonObj = jsonArray.getJSONObject(x); map.put("name", jsonObj.getString("name")); map.put("age", jsonObj.getInt("age")); map.put("salary", jsonObj.getDouble("salary")); map.put("married", jsonObj.getBoolean("married")); map.put("birthday", jsonObj.getString("birthday")); lists.add(map); } maps.put("persondata", lists); return maps; } }