JSON

Jackson学习一之对象与JSON互相转化

字号+ 作者:H5之家 来源:H5之家 2018-01-18 18:03 我要评论( )

简介: Jackson是一个流行的基于Java的JSON处理框架 官方wiki:http://wiki.fasterxml.com/JacksonInFiveMinutes 官方文档:http://wiki.fasterxml.com/JacksonDoc

简介:

Jackson是一个流行的基于Java的JSON处理框架

官方wiki:

官方文档:

学习参考:Json转换利器Gson之实例系列,顺便也学习一下GSON

本篇简介如何使用Jackson进行Java对象与JSON的互相转换

新建基本Student类:

package com.jingshou.pojo; import java.util.Date; 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 + ",,]"; } }

班级类:

package com.jingshou.pojo; import java.util.List; public class Class { private String cname; private List<Student> members; public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public List<Student> getMembers() { return members; } public void setMembers(List<Student> members) { this.members = members; } public String toString(){ return "The Class Name is: " + cname + " " + "The members are: " + members; } }

测试代码:

package com.jingshou.jackson; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.fasterxml.jackson.databind.ObjectMapper; import com.jingshou.pojo.Class; import com.jingshou.pojo.Student; public class JacksonTest1 { public static void main(String[] args) throws IOException { Student student1 = new Student(); student1.setId(5237); student1.setName("jingshou"); student1.setBirthDay(new Date()); ObjectMapper mapper = new ObjectMapper(); // Convert object to JSON string String Json = mapper.writeValueAsString(student1); System.out.println("Change Object to JSON String: " + Json); // Convert Json string to Object Student student2 = mapper.readValue(Json, Student.class); System.out.println(student2); // Create a student list add it to Bj Student student3 = new Student(); student3.setId(5117); student3.setName("saiya"); student3.setBirthDay(new Date()); List<Student> stuList = new ArrayList<Student>(); stuList.add(student1); stuList.add(student3); Class bj = new Class(); bj.setCname("五年二班"); bj.setMembers(stuList); String json2 = mapper.writeValueAsString(bj); System.out.println("The JSON from Class is: " + json2); Class bj2 = mapper.readValue(json2, Class.class); System.out.println(bj2); } }

运行结果:

Change Object to JSON String: {"id":5237,"name":"jingshou","birthDay":1389513906622} Student [birthDay=Sun Jan 12 16:05:06 CST 2014, id=5237, name=jingshou] The JSON from Class is: {"cname":"五年二班","members":[{"id":5237,"name":"jingshou","birthDay":1389513906622},{"id":5117,"name":"saiya","birthDay":1389513906859}]} The Class Name is: 五年二班 The members are: [Student [birthDay=Sun Jan 12 16:05:06 CST 2014, id=5237, name=jingshou], Student [birthDay=Sun Jan 12 16:05:06 CST 2014, id=5117, name=saiya]]

小结:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Jackson将json string转为Object,org.json读取json数组

    Jackson将json string转为Object,org.json读取json数组

    2017-12-16 11:00

  • 【学习】spring MVC之返回JSON数据(Spring3.0 MVC+Jackson+AJAX)

    【学习】spring MVC之返回JSON数据(Spring3.0 MVC+Jackson+AJAX)

    2017-12-10 12:01

  • Jackson 通过自定义注解来控制json key的格式

    Jackson 通过自定义注解来控制json key的格式

    2017-12-06 10:05

  • Jackson注解学习参考

    Jackson注解学习参考

    2017-11-17 18:06

网友点评