版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/49519209
Jackson 2开发详解
作者:chszs,转载需注明。博客主页:http://blog.csdn.net/chszs
Jackson 2库是流行的Java JSON解析库之一。本文主要讲述Jackson 2库的基本用法。
1、Jackson 2库
目前Jackson 2库的最新版本为2.6.3版。使用它需要以下依赖包。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.3</version>
</dependency>
基本的Java对象User.java
package json.ch.test;
public class User {
private String name;
private int year;
private String describe;
public User() {
super();
}
public User(String name, int year, String describe) {
super();
this.name = name;
this.year = year;
this.describe = describe;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
@Override
public String toString() {
return "User [name=" + name + ", year=" + year + ", describe=" + describe + "]";
}
}
2、Java对象转换为JSON
使用writeValue(…)方法
ObjectMapper mapper = new ObjectMapper();
User user = new User("Zhang san", 20, "He is a young boy.");
try {
mapper.writeValue(new File("t1.json"), user);
} catch (IOException e) {
e.printStackTrace();
}
try {
String jsonString = mapper.writeValueAsString(user);
System.out.println(jsonString);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
3、JSON字符串转换为Java对象
使用readValue(…)方法
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"name\":\"Zhang san\",\"year\":20,\"describe\":\"He is a young boy.\"}";
User user;
try {
user = mapper.readValue(jsonString, User.class);
System.out.println(user.toString());
} catch (IOException e) {
e.printStackTrace();
}
try {
User user1 = mapper.readValue(new File("t1.json"), User.class);
System.out.println(user1.toString());
} catch (IOException e) {
e.printStackTrace();
}
4、@JsonView注释
@JsonView注释是一个非常有用的注释,它用于控制对象中的字段。
重写User.java
package json.ch.test;
import com.fasterxml.jackson.annotation.JsonView;
public class User {
@JsonView(Views.Normal.class)
private String name;
@JsonView(Views.Normal.class)
private int year;
@JsonView(Views.Manager.class)
private String describe;
public User() {
super();
}
public User(String name, int year, String describe) {
super();
this.name = name;
this.year = year;
this.describe = describe;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
@Override
public String toString() {
return "User [name=" + name + ", year=" + year + ", describe=" + describe + "]";
}
}
定义Views.java
package json.ch.test;
public class Views {
public static class Normal{};
public static class Manager extends Normal{};
}
测试代码:
package json.ch.test;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test3 {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
User user = new User("Zhang san", 20, "He is a young boy.");
try{
System.out.println("Normal View");
String normalView = mapper.writerWithView(Views.Normal.class)
.writeValueAsString(user);
System.out.println(normalView);
String json1 = "{\"name\":\"Zhang san\",\"year\":20}";
User normalUser = mapper.readerWithView(Views.Normal.class)
.forType(User.class).readValue(json1);
System.out.println(normalUser);
System.out.println("Manager View");
String managerView = mapper.writerWithView(Views.Manager.class)
.writeValueAsString(user);
System.out.println(managerView);
String json2 = "{\"name\":\"Zhang san\",\"year\":20,\"describe\":\"He is a young boy.\"}";
User managerUser = mapper.readerWithView(Views.Manager.class)
.forType(User.class).readValue(json2);
System.out.println(managerUser.toString());
}catch(Exception e){
e.printStackTrace();
}
}
}
输出结果:
Normal View
{"name":"Zhang san","year":20}
User [name=Zhang san, year=20, describe=null]
Manager View
{"name":"Zhang san","year":20,"describe":"He is a young boy."}
User [name=Zhang san, year=20, describe=He is a young boy.]