android中JSON数据的读写步骤
android中JSON数据的读写方法[/color]用习惯了XML,总觉得JSON可读性又差,也不好保存等等,到真正接触了,才
android中JSON数据的读写方法
[/color]用习惯了XML,总觉得JSON可读性又差,也不好保存等等,到真正接触了,才发现在android上,JSON就像亲儿子一样容易管教。
还是先看数据源,数据源我采用的是一系列的村民集合,村民的属性如下。
public class Folk {String name;String career;String age;int salary;boolean sex;public class MyJsonWriter {ArrayList<Folk> folks;File saveFile;public MyJsonWriter(ArrayList<Folk> folks){this.folks=folks;}public void setFilePath(String filepath){saveFile=new File(filepath);try {saveFile.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public String getJsonData(){String jsonData = null;//String jsonData=new JSONStringer().object().key("village").value("abc").endObject().toString();try {StringBuilder builder=new StringBuilder();ArrayList<String> folksData=new ArrayList<String>();JSONArray array=new JSONArray();for(int i=0;i<folks.size();i++){Folk folk=folks.get(i);JSONObject jsonObject=new JSONObject();jsonObject.put("name", folk.getName());jsonObject.put("sex", folk.isSex()?"male":"female");jsonObject.put("age", folk.getAge());jsonObject.put("career", folk.getCareer());jsonObject.put("salary", folk.getSalary());folksData.add(jsonObject.toString());array.put(jsonObject);}//JSONArray jsonArray=new JSONArray(folksData);int len =array.length();jsonData=new JSONStringer().object().key("vallage").value(array).endObject().toString();System.out.println(jsonData);writeData(jsonData);} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}return jsonData;}private void writeData(String jsonData) {// TODO Auto-generated method stubtry {BufferedReader reader=new BufferedReader(new StringReader(jsonData));BufferedWriter writer=new BufferedWriter(new FileWriter(saveFile));int len=0;char[] buffer=new char[1024]; while((len=reader.read(buffer))!=-1){writer.write(buffer, 0, len);}writer.flush();writer.close();reader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
public class MyJsonReader {String jsonData;//ArrayList<Folk> folks;public MyJsonReader(String jsonData){this.jsonData=jsonData;}public ArrayList<Folk> getJsonData(){ArrayList<Folk> folks=new ArrayList<Folk>();try {JSONObject jsonObject=new JSONObject(jsonData);JSONArray jsonArray=jsonObject.getJSONArray("vallage");int len = jsonArray.length();for(int i=0;i<jsonArray.length();i++){JSONObject json=jsonArray.getJSONObject(i);Folk folk=new Folk();folk.setName(json.optString("name"));folk.setAge(json.optString("age"));folk.setCareer(json.optString("career"));folk.setSalary(json.optInt("salary"));folk.setSex(json.optString("sex").equals("male")?true:false);folks.add(folk);}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}return folks;}}
JSON的读写在android很简单,出奇的简单,让我这个准备花一个周五下午的人后面变得十分蛋疼的来写日志。
总的来说还有些小地方需要注意
JSONArray中添加json对象的方法不是用的add而是put
jsonData=new JSONStringer().object().key("vallage").value(array).endObject().toString();.object和endObject成对出现 这很像XML写里面的starttag和endtag
最后我将一些的村民数据做为了一个village json对象的value,真的把这个Json对象幻想成了一艘宇宙飞船。。。