分享

gson使用在android使用例子

 w_hf的图书馆 2011-08-15
gson使用在android使用例子
Java代码 复制代码 收藏代码
  1. 虽然android自带了json处理,但是没有封装,总觉得不方便,网上找了gson的处理,觉得还行。
Java代码 复制代码 收藏代码
  1. //转换器
  2. GsonBuilder builder = new GsonBuilder();
  3. // 不转换没有 @Expose 注解的字段
  4. builder.excludeFieldsWithoutExposeAnnotation();
  5. Gson gson = builder.create();
  6. //1、对象转string
  7. Student stu = new Student();
  8. stu.setStudentId(333);
  9. stu.setStudentName("qqq");
  10. String stuStr = gson.toJson(stu);
  11. System.out.println(stuStr); //{"studentName":"qqq","studentId":333}
  12. //2、string转对象
  13. Student user2 = gson.fromJson(stuStr, Student.class);
  14. System.out.println(user2);
  15. String stuTemp = "{\"studentName\":\"qqq2\",\"studentId\":3335}";
  16. Student user4 = gson.fromJson(stuTemp, Student.class);
  17. System.out.println(user4);
  18. //3、对象List转string
  19. List<Student> testBeanList = new ArrayList<Student>();
  20. Student testBean = new Student();
  21. testBean.setStudentId(555);
  22. testBean.setStudentName("552");
  23. testBeanList.add(testBean);
  24. //Gson gsonList = new Gson();
  25. Type type = new TypeToken<List<Student>>(){}.getType(); //指定集合对象属性
  26. String beanListToJson = gson.toJson(testBeanList, type);
  27. System.out.println(beanListToJson); //[{"studentName":"552","studentId":555}]
  28. //集合string转对象list
  29. List<Student> testBeanListFromJson = gson.fromJson(beanListToJson, type);
  30. System.out.println(testBeanListFromJson); //[555:552]
  31. //4、集合如果不指定类型 默认为String
  32. List<String> testList = new ArrayList<String>();
  33. testList.add("first");
  34. testList.add("second");
  35. String listToJson = gson.toJson(testList);
  36. System.out.println(listToJson); //["first","second"]
  37. //5、集合字符串转回来需要指定类型
  38. List<String> testList2 = (List<String>) gson.fromJson(listToJson,
  39. new TypeToken<List<String>>() {
  40. }.getType());
  41. System.out.println(testList2);
  42. //6、 将HashMap字符串转换为 JSON
  43. Map<String, String> testMap = new HashMap<String, String>();
  44. testMap.put("id", "id.first");
  45. testMap.put("name", "name.second");
  46. String mapToJson = gson.toJson(testMap);
  47. System.out.println(mapToJson); //{"id":"id.first","name":"name.second"}
  48. //7、stringMap转对象
  49. Map<String, String> userMap2 = (Map<String, String>) gson.fromJson(mapToJson,
  50. new TypeToken<Map<String, String>>() {
  51. }.getType());
  52. System.out.println(userMap2); //{id=id.first, name=name.second}
  53. //8、对象含有普通对象、集合、map情况
  54. Student user1 = new Student();
  55. user1.setStudentId(1001);
  56. user1.setStudentName("张三");
  57. Student user3 = new Student();
  58. user3.setStudentId(1002);
  59. user3.setStudentName("李四");
  60. Map<String, Student> userMap = new HashMap<String, Student>();
  61. userMap.put("user1", user1);
  62. userMap.put("user3", user3);
  63. List<Student> userList = new ArrayList<Student>();
  64. userList.add(user1);
  65. userList.add(user3);
  66. Teacher groupBean = new Teacher();
  67. groupBean.setStudent(user1);
  68. groupBean.setStus(userList);
  69. groupBean.setMap((HashMap)userMap);
  70. //groupBean.setUserList(userList);
  71. Gson gsonGroup = new Gson();
  72. String sGroupBean = gsonGroup.toJson(groupBean, new TypeToken<Teacher>() {
  73. }.getType());
  74. System.out.println(sGroupBean);
  75. /*{"stus":[{"studentName":"张三","studentId":1001},{"studentName":"李四","studentId":1002}],"student":{"studentName":"张三","studentId":1001},"map":{"user3":{"studentName":"李四","studentId":1002},"user1":{"studentName":"张三","studentId":1001}},"id":0,"age":0}*/
Java代码 复制代码 收藏代码
  1. //9、复杂对象string转对象
  2. Teacher groupBean2 = (Teacher) gson.fromJson(sGroupBean,
  3. new TypeToken<Teacher>() {
  4. }.getType());
  5. System.out.println(groupBean2);

 

Java代码 复制代码 收藏代码
  1. package com.andtools;
  2. import com.google.gson.annotations.Expose;
  3. public class Student {
  4. @Expose
  5. private String studentName;
  6. @Expose
  7. private int studentId;
  8. public Student(){}
  9. public Student(int studentId,String studentName){
  10. this.setStudentId(studentId);
  11. this.setStudentName(studentName);
  12. }
  13. public String toString(){
  14. return this.getStudentId() + ":" + this.getStudentName();
  15. }
  16. public String getStudentName() {
  17. return studentName;
  18. }
  19. public void setStudentName(String studentName) {
  20. this.studentName = studentName;
  21. }
  22. public int getStudentId() {
  23. return studentId;
  24. }
  25. public void setStudentId(int studentId) {
  26. this.studentId = studentId;
  27. }
  28. }

 

Java代码 复制代码 收藏代码
  1. package com.andtools;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import com.google.gson.annotations.Expose;
  6. public class Teacher {
  7. @Expose
  8. private int id;
  9. @Expose
  10. private String name;
  11. @Expose
  12. private int age;
  13. @Expose
  14. private Student student;
  15. @Expose
  16. private List stus;
  17. @Expose
  18. private HashMap map;
  19. public String toString(){
  20. return this.getId()+":"+this.getName()+":"+this.getAge() +":"+ this.getStudent().toString() + ":" + this.getStus() + ":" + this.getMap();
  21. }
  22. public int getId() {
  23. return id;
  24. }
  25. public void setId(int id) {
  26. this.id = id;
  27. }
  28. public String getName() {
  29. return name;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public int getAge() {
  35. return age;
  36. }
  37. public void setAge(int age) {
  38. this.age = age;
  39. }
  40. public Student getStudent() {
  41. return student;
  42. }
  43. public void setStudent(Student student) {
  44. this.student = student;
  45. }
  46. public List getStus() {
  47. return stus;
  48. }
  49. public void setStus(List stus) {
  50. this.stus = stus;
  51. }
  52. public HashMap getMap() {
  53. return map;
  54. }
  55. public void setMap(HashMap map) {
  56. this.map = map;
  57. }
  58. }

 

附件为网上找的文档。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多