Android开源框架——网络解析GSON
生活随笔
收集整理的這篇文章主要介紹了
Android开源框架——网络解析GSON
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
準備工作
GSON下載地址:http://download.csdn.net/detail/wiseclown/9496184
官網地址:https://github.com/google/gson
JavaBean(自定義):
public class Student {private String id;private String name;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;} }讀JSON數(shù)據
//單個對象 String json = "{\"id\":\"0001\",\"name\":\"zs\",\"sex\":\"male\"}"; Gson gson = new Gson(); Student student = gson.fromJson(json,Student.class);//多個對象json數(shù)組 String json = "[{\"id\":\"0001\",\"name\":\"zs\",\"sex\":\"male\"},{\"id\":\"0002\",\"name\":\"ls\",\"sex\":\"male\"}]"; Gson gson = new Gson(); List<Student> studentList = gson.fromJson(json,new TypeToken<List<Student>)(){}.getType();從文件中讀取JSON數(shù)據
student2.json
[{“id”:”000001”,”name”:”wz”,”sex”:”male”},
?{“id”:”000002”,”name”:”zs”,”sex”:”male”},
?{“id”:”000003”,”name”:”wf”,”sex”:”female”}
]
將JSON數(shù)據寫入文件中
Student student = new Student(); student.setId("1"); student.setName("wz"); student.setSex("male"); Gson gson = new Gson(); String jsonStr = gson.toJson(student);File file = new File(Environment.getExternalStorageDirectory(),"student.json"); try{FileOutputStream out = new FileOutputStream(file);out.write(jsonStr.getBytes("UTF-8")); } catch (FileNotFoundException e) {e.printStackTrace(); } catch (IOException e) {e.printStackTrace(); }總結
以上是生活随笔為你收集整理的Android开源框架——网络解析GSON的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JSP中include指令的乱码问题
- 下一篇: Intent传递对象