Java下利用Jackson進行JSON解析和序列化示例
Java下常見的Json類庫有Gson、JSON-lib和Jackson等,Jackson相對來說比較高效,在項目中主要使用Jackson進行JSON和Java對象轉(zhuǎn)換,下面給出一些Jackson的JSON操作方法。
一、準備工作
首先去官網(wǎng)下載Jackson工具包。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對象轉(zhuǎn)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("xiaomin@sina.com");
user.setAge(20);
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
user.setBirthday(dateformat.parse("1996-10-01"));
/**
* ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中實現(xiàn)。
* ObjectMapper有多個JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介質(zhì)中。
* writeValue(File arg0, Object arg1)把arg1轉(zhuǎn)成json序列,并保存到arg0文件中。
* writeValue(OutputStream arg0, Object arg1)把arg1轉(zhuǎn)成json序列,并保存到arg0輸出流中。
* writeValueAsBytes(Object arg0)把arg0轉(zhuǎn)成json序列,并把結(jié)果輸出成字節(jié)數(shù)組。
* writeValueAsString(Object arg0)把arg0轉(zhuǎn)成json序列,并把結(jié)果輸出成字符串。
*/
ObjectMapper mapper = new ObjectMapper();
//User類轉(zhuǎn)JSON
//輸出結(jié)果:{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}
String json = mapper.writeValueAsString(user);
System.out.println(json);
//Java集合轉(zhuǎn)JSON
//輸出結(jié)果:[{"name":"小民","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);
}
}
三、JSON轉(zhuǎn)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\":\"xiaomin@sina.com\"}";
/**
* ObjectMapper支持從byte[]、File、InputStream、字符串等數(shù)據(jù)的JSON反序列化。
*/
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
System.out.println(user);
}
}
四、JSON注解
Jackson提供了一系列注解,方便對JSON序列化和反序列化進行控制,下面介紹一些常用的注解。
@JsonIgnore 此注解用于屬性上,作用是進行JSON操作時忽略該屬性。
@JsonFormat 此注解用于屬性上,作用是把Date類型直接轉(zhuǎn)化為想要的格式,如@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("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);
//輸出結(jié)果:{"name":"小民","birthday":"1996年09月30日","mail":"xiaomin@sina.com"}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaWeb連接數(shù)據(jù)庫MySQL的操作技巧
數(shù)據(jù)庫是編程中重要的一部分,它囊括了數(shù)據(jù)操作,數(shù)據(jù)持久化等各方面。在每一門編程語言中都占有相當大的比例。本次,小編以MySQL為例,使用mvc編程思想,給大家講解下javaweb對數(shù)據(jù)庫的操作2017-02-02
Mybatis-Plus條件構(gòu)造器select方法返回指定字段方式
這篇文章主要介紹了Mybatis-Plus條件構(gòu)造器select方法返回指定字段方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Intellij Idea插件開發(fā)之創(chuàng)建項目層級的右鍵菜單
這篇文章主要介紹了Intellij Idea插件開發(fā)之創(chuàng)建項目層級的右鍵菜單,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02

