发现了google的gson,因为之前对于protocolbuf有一些了解,带着一些好奇心,我开始使用了gson。
GitHub主页:https://github.com/google/gson
经过比较,gson和其他现有java json类库最大的不同时gson需要序列化得实体类不需要使用annotation来标识需要序列化得字段,同时gson又可以通过使用annotation来灵活配置需要序列化的字段。
将List或者Map转换成json非常简单:
public String getJsonData(List<?> list) { Gson gson = new Gson(); String jsonstring = gson.toJson(list); return jsonstring; }
下面我们会有详细的例子。
示例
简单对象转化和带泛型的List转化:
实体类:
public class Student { private int id; private String name; private Date birthDay; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } @Override public String toString() { return "Student [birthDay=" + birthDay + ", id=" + id + ", name=" + name + "]"; } }
测试类:
import java.util.ArrayList; import java.util.Date; import java.util.List; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonTest1 { public static void main(String[] args) { Gson gson = new Gson(); Student student1 = new Student(); student1.setId(1); student1.setName("李坤"); student1.setBirthDay(new Date()); // ////////////////////////////////////////////////////////// System.out.println("----------简单对象之间的转化-------------"); // 简单的bean转为json String s1 = gson.toJson(student1); System.out.println("简单Bean转化为Json===" + s1); // json转为简单Bean Student student = gson.fromJson(s1, Student.class); System.out.println("Json转为简单Bean===" + student); // 结果: // 简单Bean转化为Json==={"id":1,"name":"李坤","birthDay":"Jun 22, 2012 8:27:52 AM"} // Json转为简单Bean===Student [birthDay=Fri Jun 22 08:27:52 CST 2012, id=1, // name=李坤] // ////////////////////////////////////////////////////////// Student student2 = new Student(); student2.setId(2); student2.setName("曹贵生"); student2.setBirthDay(new Date()); Student student3 = new Student(); student3.setId(3); student3.setName("柳波"); student3.setBirthDay(new Date()); List<Student> list = new ArrayList<Student>(); list.add(student1); list.add(student2); list.add(student3); System.out.println("----------带泛型的List之间的转化-------------"); // 带泛型的list转化为json String s2 = gson.toJson(list); System.out.println("带泛型的list转化为json==" + s2); // json转为带泛型的list List<Student> retList = gson.fromJson(s2, new TypeToken<List<Student>>() { }.getType()); for (Student stu : retList) { System.out.println(stu); } // 结果: // 带泛型的list转化为json==[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 8:28:52 AM"},{"id":2,"name":"曹贵生","birthDay":"Jun 22, 2012 8:28:52 AM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 8:28:52 AM"}] // Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=1, name=李坤] // Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=2, name=曹贵生] // Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=3, name=柳波] } }
执行结果:
----------简单对象之间的转化------------- 简单Bean转化为Json==={"id":1,"name":"李坤","birthDay":"Jun 22, 2012 9:10:31 PM"} Json转为简单Bean===Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=1, name=李坤] ----------带泛型的List之间的转化------------- 带泛型的list转化为json==[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 9:10:31 PM"},{"id":2,"name":"曹贵生","birthDay":"Jun 22, 2012 9:10:31 PM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 9:10:31 PM"}] Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=1, name=李坤] Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=2, name=曹贵生] Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=3, name=柳波]
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。