1、jackson能做什么?
Jackson 被称为 "Java JSON 库 "或 "Java 的最佳 JSON 解析器"。不仅如此,Jackson 还是一套适用于 Java(和 JVM 平台)的数据处理工具,除了能处理json格式数据以外,
还能处理csv、xml、(java)properties等格式数据。
2、使用方法
- 处理json数据
- maven中加入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>maven_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<jackson.version>2.13.3</jackson.version>
</properties>
<dependencies>
<!--<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
</project>
- 序列化
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test1 {
static ObjectMapper objectMapper = new ObjectMapper();
static {
// 序列化时空值属性不序列化
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
// jackson在反序列化时默认如果遇到未知属性(没有映射到属性并且没有setter或者handler来处理它)时会报错,可以通过以下配置解决
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
}
@Test
public void test1() throws JsonProcessingException {
MyBean myBean = new MyBean();
myBean.setId(1);
myBean.setName("My name");
// bean
String s = objectMapper.writeValueAsString(myBean);
System.out.println(s);
List<MyBean> list = new ArrayList<>();
list.add(myBean);
// list
String s1 = objectMapper.writeValueAsString(list);
System.out.println(s1);
// map
Map<String,Object> map = new HashMap<>();
map.put("id",2);
map.put("name","your name");
String s2 = objectMapper.writeValueAsString(map);
System.out.println(s2);
}
}
class MyBean {
public Integer id;
public String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "MyBean{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
- 反序列化
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test1 {
static ObjectMapper objectMapper = new ObjectMapper();
static {
// 序列化时空值属性不序列化
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
// jackson在反序列化时默认如果遇到未知属性(没有映射到属性并且没有setter或者handler来处理它)时会报错,可以通过以下配置解决
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
}
@Test
public void test2() throws JsonProcessingException {
String jsonStr = "{\"id\":1,\"name\":\"My name\"}";
MyBean myBean = objectMapper.readValue(jsonStr, MyBean.class);
System.out.println(myBean);
String jsonStr1 = "[{\"id\":1,\"name\":\"My name\"}]";
// 指定返回class为List.class的话,list中的类型是map类型
List list = objectMapper.readValue(jsonStr1, List.class);
System.out.println(list);
// 使用TypeReference指定反序列化后的类型
List<MyBean> list1 = objectMapper.readValue(jsonStr1, new TypeReference<List<MyBean>>() {});
System.out.println(list1.get(0));
// 使用TypeFactory构建反序列化后的类型
CollectionType collectionType = objectMapper.getTypeFactory().constructCollectionType(List.class, MyBean.class);
List<MyBean> list2 = objectMapper.readValue(jsonStr1, collectionType);
System.out.println(list2.get(0));
}
}
class MyBean {
public Integer id;
public String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "MyBean{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
标签:jackson,name,public,使用,import,id,fasterxml
From: https://www.cnblogs.com/mmcode/p/17660021.html