Jackson框架是基于Java平台的一套数据处理工具,它包含了3个核心库:streaming,databind,annotations,被称为“最好的Java Json解析器”。
Jackson下载
jackon包含三个核心类库:
jackson-core-2.14.1.jar 定义了底层的streaming API和实现了Json特性。
jackson-annotations-2.14.1.jar 包含了标准的Jackson注解。
jackson-databind-2.14.1.jar 实现了数据绑定和对象序列化,它依赖于streaming和annotations的包。
Jackson安装
1. Idea 中导入json包的方法。ctrl+alt+shift+S 打开项目结构
2. maven安装
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.14.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.14.1</version>
</dependency>
Java中Jackson将json转换成对象
json转Java对象
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
/**
*JSON转java对象
*/
public class JSON2Class{
//JSON转java对象
public static void main(String[] args) {
try {
ObjectMapper objectMapper = new ObjectMapper();
String json = "{\"name\":\"张三\",\"age\":23,\"address\":\"深圳市\"}";
User zhang = objectMapper.readValue(json, User.class);
System.out.println(zhang.getName() + "\n" + zhang.getAge() + "\n" + zhang.getAddress());
} catch(IOException e) {
e.printStackTrace();
}
}
}
//User.java
class User {
private String name;
private Integer age;
private String address;
public User(){
}
public User(String name, Integer age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
输出张三
23
深圳市
23
深圳市
json转list对象
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*2. json转list对象
*/
public class JSON2List {
//JSON转java对象
public static void main(String[] args) {
try {
ObjectMapper objectMapper = new ObjectMapper();
//JSON转list
String listJson = "[{\"name\":\"张三\",\"age\":21,\"address\":\"深圳\"}," + "{\"name\":\"李四\",\"age\":11,\"address\":\"武汉\"}," + "{\"name\":\"王五\",\"age\":31,\"address\":\"北京\"}]";
JavaType javaType1 = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, User.class);
List< User > userList = (List < User > ) objectMapper.readValue(listJson, javaType1);
for (User user1: userList) {
System.out.println(user1.getName() + "\t" + user1.getAge() + "\t" + user1.getAddress());
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
//User.java
class User {
private String name;
private Integer age;
private String address;
public User(){
}
public User(String name, Integer age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
输出张三 21 深圳
李四 11 武汉
王五 31 北京
李四 11 武汉
王五 31 北京
json转map对象
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
*3. json转map对象
*/
public class json2map {
//JSON转java对象
public static void main(String[] args) {
try {
ObjectMapper objectMapper = new ObjectMapper();
//JSON转map
String mapJson = "{\"one\":{\"name\":\"张三\",\"age\":21,\"address\":\"深圳\"}," + "\"two\":{\"name\":\"李四\",\"age\":11,\"address\":\"武汉\"}," + "\"three\":{\"name\":\"王五\",\"age\":31,\"address\":\"北京\"}}";
JavaType javaType2 = objectMapper.getTypeFactory().constructParametricType(HashMap.class, String.class, User.class);
Map < String,User > userMap = (Map< String, User >) objectMapper.readValue(mapJson, javaType2);
User one = userMap.get("one");
User two = userMap.get("two");
User three = userMap.get("three");
System.out.println(one.getName() + "\t" + one.getAge() + "\t" + one.getAddress());
System.out.println(two.getName() + "\t" + two.getAge() + "\t" + two.getAddress());
System.out.println(three.getName() + "\t" + three.getAge() + "\t" + three.getAddress());
} catch(IOException e) {
e.printStackTrace();
}
}
}
//User.java
class User {
private String name;
private Integer age;
private String address;
public User(){
}
public User(String name, Integer age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Java中Jackson将对象转换成json
map对象转换成json
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
*1. map对象转换成json
*/
public class map2json {
//JSON转java对象
public static void main(String[] args) {
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
//map转JSON
Map < String,Object > map = new HashMap < String,Object > ();
map.put("one", new User("张", 12, "深圳"));
map.put("two", new User("李", 22, "武汉"));
map.put("three", new User("王", 32, "北京"));
System.out.println("ObjectMapper方式");
objectMapper.writeValue(System.out, map);
System.out.println("JsonGenerator方式");
jsonGenerator.writeObject(map);
} catch(IOException e) {
e.printStackTrace();
}
}
}
//User.java
class User {
private String name;
private Integer age;
private String address;
public User(){
}
public User(String name, Integer age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
输出ObjectMapper方式
{"one":{"name":"张","age":12,"address":"深圳"},"two":{"name":"李","age":22,"address":"武汉"},"three":{"name":"王","age":32,"address":"北京"}}
{"one":{"name":"张","age":12,"address":"深圳"},"two":{"name":"李","age":22,"address":"武汉"},"three":{"name":"王","age":32,"address":"北京"}}
list对象转换成json
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*2. list对象转换成json
*/
public class ListToJSON {
//JSON转java对象
public static void main(String[] args) {
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
//list转JSON
List< User > list = new ArrayList< User >();
list.add(new User("张", 12, "深圳"));
list.add(new User("李", 22, "武汉"));
list.add(new User("王", 32, "北京"));
System.out.println("ObjectMapper方式");
objectMapper.writeValue(System.out, list);
System.out.println("JsonGenerator方式");
jsonGenerator.writeObject(list);
} catch(IOException e) {
e.printStackTrace();
}
}
}
//User.java
class User {
private String name;
private Integer age;
private String address;
public User(){
}
public User(String name, Integer age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}