原文出處:http://blog.csdn.net/zmx729618/article/details/52161069
一、準備工作
首先去官網下載Jackson工具包,下載地址http://wiki.fasterxml.com/JacksonDownload。Jackson有1.x系列和2.x系列,截止目前2.x系列的最新版本是2.2.3,2.x系列有3個jar包需要下載:
jackson-core-2.2.3.jar(核心jar包,下載地址)
jackson-annotations-2.2.3.jar(該包提供Json註解支持,下載地址)
jackson-databind-2.2.3.jar(下載地址)
- //JSON序列化和反序列化使用的User類
- import java.util.Date;
- public class User {
- private String name;
- private Integer age;
- private Date birthday;
- private String email;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- }
二、JAVA對象轉JSON[JSON序列化]
- import java.io.IOException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import com.fasterxml.jackson.databind.ObjectMapper;
- public class JacksonDemo {
- public static void main(String[] args) throws ParseException, IOException {
- User user = new User();
- user.setName("小民");
- user.setEmail("[email protected]");
- user.setAge(20);
- SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
- user.setBirthday(dateformat.parse("1996-10-01"));
- /**
- * ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中實現。
- * ObjectMapper有多個JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介質中。
- * writeValue(File arg0, Object arg1)把arg1轉成json序列,並保存到arg0文件中。
- * writeValue(OutputStream arg0, Object arg1)把arg1轉成json序列,並保存到arg0輸出流中。
- * writeValueAsBytes(Object arg0)把arg0轉成json序列,並把結果輸出成字節數組。
- * writeValueAsString(Object arg0)把arg0轉成json序列,並把結果輸出成字符串。
- */
- ObjectMapper mapper = new ObjectMapper();
- //User類轉JSON
- //輸出結果:{"name":"小民","age":20,"birthday":844099200000,"email":"[email protected]"}
- String json = mapper.writeValueAsString(user);
- System.out.println(json);
- //Java集合轉JSON
- //輸出結果:[{"name":"小民","age":20,"birthday":844099200000,"email":"[email protected]"}]
- List<User> users = new ArrayList<User>();
- users.add(user);
- String jsonlist = mapper.writeValueAsString(users);
- System.out.println(jsonlist);
- }
- }
三、JSON轉Java類[JSON反序列化]
- import java.io.IOException;
- import java.text.ParseException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- public class JacksonDemo {
- public static void main(String[] args) throws ParseException, IOException {
- String json = "{\"name\":\"小民\",\"age\":20,\"birthday\":844099200000,\"email\":\"[email protected]\"}";
- /**
- * ObjectMapper支持從byte[]、File、InputStream、字符串等數據的JSON反序列化。
- */
- ObjectMapper mapper = new ObjectMapper();
- User user = mapper.readValue(json, User.class);
- System.out.println(user);
- }
- }
四、JSON註解
Jackson提供了一系列註解,方便對JSON序列化和反序列化進行控制,下面介紹一些常用的註解。
@JsonIgnore 此註解用於屬性上,作用是進行JSON操作時忽略該屬性。
@JsonFormat 此註解用於屬性上,作用是把Date類型直接轉化爲想要的格式,如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")。
@JsonProperty 此註解用於屬性上,作用是把該屬性的名稱序列化爲另外一個名稱,如把trueName屬性序列化爲name,@JsonProperty("name")。
- import java.util.Date;
- import com.fasterxml.jackson.annotation.*;
- public class User {
- private String name;
- //不JSON序列化年齡屬性
- @JsonIgnore
- private Integer age;
- //格式化日期屬性
- @JsonFormat(pattern = "yyyy年MM月dd日")
- private Date birthday;
- //序列化email屬性爲mail
- @JsonProperty("mail")
- private String email;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- }
- import java.io.IOException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import com.fasterxml.jackson.databind.ObjectMapper;
- public class JacksonDemo {
- public static void main(String[] args) throws ParseException, IOException {
- User user = new User();
- user.setName("小民");
- user.setEmail("[email protected]");
- user.setAge(20);
- SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
- user.setBirthday(dateformat.parse("1996-10-01"));
- ObjectMapper mapper = new ObjectMapper();
- String json = mapper.writeValueAsString(user);
- System.out.println(json);
- //輸出結果:{"name":"小民","birthday":"1996年09月30日","mail":"[email protected]"}
- }
- }