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; } }
相关推荐
Android_JSON数据解析 博客里面的Demo
自己写的json解析,不使用第三方的jar包,简单易懂,分为本地json解析和online json解析,网上的只需要修改一下url地址即可
Android XML和JSON解析的代码,学习笔记。
android封装的json解析,可以直接解析对象或者list 如果有不明白的地方,可以联系我: QQ:840950105 Email:vipa1888@163.com Author:spring sky
android json 解析 json数据格式解析我自己分为两种; 一种是普通的,一种是带有数组形式的;
Android解析——json解析为javabean
利用GET请求获取到JSON数据,然后再Android Studio中对JSON数组进行解析,获取到的内容展示再ListView上,比较适合萌新学习解析JSON数组,每一个步骤都有注释,非常的详细。
android框架解析JSON数据放入ListView的例子
Android Json 解析demo 建立,基于Android stdio
android的json解析,三种解析方式
eclipse项目。获取raw文件下的json文件。无需编写json数据里面key值的实体类,动态获取里面的键值对的值。并在列表显示
Android JSON解析
在Android Studio中通过GET请求到的JSON对象的解析源码,每一步都有注释,方便小白萌新学习。
Android中Json的数据解析博文源码。博文请关注:http://blog.csdn.net/yegongheng
Android 之 Json 数据解析 完整的json解析数据方法
Android应用源码之Android 解析json_dome.zip
注释非常详细,适合新手!android客户端通过json解析web端的数据!
该Demo主要介绍使用HttpClient获取网络数据,然后经过JSON后显示出来。另外本文章用到了常用的异步加载技术,AsyncTask类。
Android开发之原生解析复杂的json数据
我们通常在Android上采用Gson来解析Json数据,很方便的就可以把数据转换成List或者map。当碰到日期时间的时候,你可能会遇到坑,需要格式化一下日期时间格式。 本文介绍org.json这种不需要导入任何包的解析方式,...