gson使用在android使用例子

转自:http://smallbee.iteye.com/blog/1066993

  1. //转换器
  2. GsonBuilderbuilder=newGsonBuilder();
  3. //不转换没有@Expose注解的字段
  4. builder.excludeFieldsWithoutExposeAnnotation();
  5. Gsongson=builder.create();
  6. //1、对象转string
  7. Studentstu=newStudent();
  8. stu.setStudentId(333);
  9. stu.setStudentName("qqq");
  10. StringstuStr=gson.toJson(stu);
  11. System.out.println(stuStr);//{"studentName":"qqq","studentId":333}
  12. //2、string转对象
  13. Studentuser2=gson.fromJson(stuStr,Student.class);
  14. System.out.println(user2);
  15. StringstuTemp="{\"studentName\":\"qqq2\",\"studentId\":3335}";
  16. Studentuser4=gson.fromJson(stuTemp,Student.class);
  17. System.out.println(user4);
  18. //3、对象List转string
  19. List<Student>testBeanList=newArrayList<Student>();
  20. StudenttestBean=newStudent();
  21. testBean.setStudentId(555);
  22. testBean.setStudentName("552");
  23. testBeanList.add(testBean);
  24. //GsongsonList=newGson();
  25. Typetype=newTypeToken<List<Student>>(){}.getType();//指定集合对象属性
  26. StringbeanListToJson=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=newArrayList<String>();
  33. testList.add("first");
  34. testList.add("second");
  35. StringlistToJson=gson.toJson(testList);
  36. System.out.println(listToJson);//["first","second"]
  37. //5、集合字符串转回来需要指定类型
  38. List<String>testList2=(List<String>)gson.fromJson(listToJson,
  39. newTypeToken<List<String>>(){
  40. }.getType());
  41. System.out.println(testList2);
  42. //6、将HashMap字符串转换为JSON
  43. Map<String,String>testMap=newHashMap<String,String>();
  44. testMap.put("id","id.first");
  45. testMap.put("name","name.second");
  46. StringmapToJson=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. newTypeToken<Map<String,String>>(){
  51. }.getType());
  52. System.out.println(userMap2);//{id=id.first,name=name.second}
  53. //8、对象含有普通对象、集合、map情况
  54. Studentuser1=newStudent();
  55. user1.setStudentId(1001);
  56. user1.setStudentName("张三");
  57. Studentuser3=newStudent();
  58. user3.setStudentId(1002);
  59. user3.setStudentName("李四");
  60. Map<String,Student>userMap=newHashMap<String,Student>();
  61. userMap.put("user1",user1);
  62. userMap.put("user3",user3);
  63. List<Student>userList=newArrayList<Student>();
  64. userList.add(user1);
  65. userList.add(user3);
  66. TeachergroupBean=newTeacher();
  67. groupBean.setStudent(user1);
  68. groupBean.setStus(userList);
  69. groupBean.setMap((HashMap)userMap);
  70. //groupBean.setUserList(userList);
  71. GsongsonGroup=newGson();
  72. StringsGroupBean=gsonGroup.toJson(groupBean,newTypeToken<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. TeachergroupBean2=(Teacher)gson.fromJson(sGroupBean,
  3. newTypeToken<Teacher>(){
  4. }.getType());
  5. System.out.println(groupBean2);

Java代码 收藏代码
  1. packagecom.andtools;
  2. importcom.google.gson.annotations.Expose;
  3. publicclassStudent{
  4. @Expose
  5. privateStringstudentName;
  6. @Expose
  7. privateintstudentId;
  8. publicStudent(){}
  9. publicStudent(intstudentId,StringstudentName){
  10. this.setStudentId(studentId);
  11. this.setStudentName(studentName);
  12. }
  13. publicStringtoString(){
  14. returnthis.getStudentId()+":"+this.getStudentName();
  15. }
  16. publicStringgetStudentName(){
  17. returnstudentName;
  18. }
  19. publicvoidsetStudentName(StringstudentName){
  20. this.studentName=studentName;
  21. }
  22. publicintgetStudentId(){
  23. returnstudentId;
  24. }
  25. publicvoidsetStudentId(intstudentId){
  26. this.studentId=studentId;
  27. }
  28. }

Java代码 收藏代码
  1. packagecom.andtools;
  2. importjava.util.HashMap;
  3. importjava.util.List;
  4. importjava.util.Map;
  5. importcom.google.gson.annotations.Expose;
  6. publicclassTeacher{
  7. @Expose
  8. privateintid;
  9. @Expose
  10. privateStringname;
  11. @Expose
  12. privateintage;
  13. @Expose
  14. privateStudentstudent;
  15. @Expose
  16. privateListstus;
  17. @Expose
  18. privateHashMapmap;
  19. publicStringtoString(){
  20. returnthis.getId()+":"+this.getName()+":"+this.getAge()+":"+this.getStudent().toString()+":"+this.getStus()+":"+this.getMap();
  21. }
  22. publicintgetId(){
  23. returnid;
  24. }
  25. publicvoidsetId(intid){
  26. this.id=id;
  27. }
  28. publicStringgetName(){
  29. returnname;
  30. }
  31. publicvoidsetName(Stringname){
  32. this.name=name;
  33. }
  34. publicintgetAge(){
  35. returnage;
  36. }
  37. publicvoidsetAge(intage){
  38. this.age=age;
  39. }
  40. publicStudentgetStudent(){
  41. returnstudent;
  42. }
  43. publicvoidsetStudent(Studentstudent){
  44. this.student=student;
  45. }
  46. publicListgetStus(){
  47. returnstus;
  48. }
  49. publicvoidsetStus(Liststus){
  50. this.stus=stus;
  51. }
  52. publicHashMapgetMap(){
  53. returnmap;
  54. }
  55. publicvoidsetMap(HashMapmap){
  56. this.map=map;
  57. }
  58. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值