Java USES Jackson for JSON parsing and serialization examples

  • 2020-06-07 04:33:43
  • OfStack

Common Json libraries under Java include Gson, ES4en-ES5en and Jackson, etc. Jackson is relatively efficient. Jackson is mainly used in the project to convert JSON and Java objects. The JSON operation methods of Jackson are given below.

1. Preparation

First go to the website and download the Jackson toolkit. Jackson has 1.x series and 2.x series. As of now, the latest version of 2.x series is 2.2.3.

jackson-core-2.2.3. jar (Core jar package, download address)

jackson-annotations-2.2.2.3.jar (this package provides SUPPORT for Json annotations, download address)

jackson-databind-2.2.3.jar (Download address)


//JSON Used for serialization and deserialization User class  
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; 
  } 
} 

2. JAVA object to JSON[JSON serialization]


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(" wang ");  
    user.setEmail("xiaomin@sina.com"); 
    user.setAge(20); 
     
    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); 
    user.setBirthday(dateformat.parse("1996-10-01"));     
     
    /** 
     * ObjectMapper is JSON The core of the operation, Jackson All of the JSON Operations are all in ObjectMapper In the implementation.  
     * ObjectMapper There are multiple JSON Serialization method can be put into JSON String saving File , OutputStream And so on in different media.  
     * writeValue(File arg0, Object arg1) the arg1 into json Sequence and save to arg0 File.  
     * writeValue(OutputStream arg0, Object arg1) the arg1 into json Sequence and save to arg0 Output stream.  
     * writeValueAsBytes(Object arg0) the arg0 into json Sequence and output the result as a byte array.  
     * writeValueAsString(Object arg0) the arg0 into json Sequence and output the result as a string.  
     */ 
    ObjectMapper mapper = new ObjectMapper(); 
     
    //User Turn class JSON 
    // Output results: {"name":" wang ","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"} 
    String json = mapper.writeValueAsString(user); 
    System.out.println(json); 
     
    //Java Set to turn JSON 
    // Output results: [{"name":" wang ","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}] 
    List<User> users = new ArrayList<User>(); 
    users.add(user); 
    String jsonlist = mapper.writeValueAsString(users); 
    System.out.println(jsonlist); 
  } 
} 

3. JSON to Java class [JSON deserialization]


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\":\" wang \",\"age\":20,\"birthday\":844099200000,\"email\":\"xiaomin@sina.com\"}"; 
     
    /** 
     * ObjectMapper Support from the byte[] , File , InputStream , string, etc JSON Deserialization.  
     */ 
    ObjectMapper mapper = new ObjectMapper(); 
    User user = mapper.readValue(json, User.class); 
    System.out.println(user); 
  } 
} 

4. JSON annotation

Jackson provides 1 series of annotations to control the serialization and deserialization of JSON. Here are 1 common annotations.

@JsonIgnore This annotation is used on a property to ignore the property when performing the JSON operation.

The @JsonFormat annotation is used on attributes to convert the Date type directly to the desired format, such as @JsonFormat (pattern = "yyyy-ES83en-ES84en HH-ES86en-ES87en ").

The @JsonProperty annotation is used on a property to serialize the name of the property to another name, such as the trueName property to name, @JsonProperty ("name").


import java.util.Date; 
import com.fasterxml.jackson.annotation.*; 
 
public class User { 
  private String name; 
   
  // Don't JSON Serialized age property  
  @JsonIgnore  
  private Integer age; 
   
  // Formatting date property  
  @JsonFormat(pattern = "yyyy years MM month dd day ") 
  private Date birthday; 
   
  // serialization email Properties for 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(" wang ");  
    user.setEmail("xiaomin@sina.com"); 
    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); 
    // Output results: {"name":" wang ","birthday":"1996 years 09 month 30 day ","mail":"xiaomin@sina.com"} 
  } 
} 

Related articles: