jackson学习(一):json字符串转自定义类数组

前言

Jackson 是一个 Java 用来处理 JSON 格式数据的类库,性能非常好。我也一直在用jackson,但是在开发过程中也遇到过一些需要花较长时间攻克的难题,我想把这些难题一一记录下来,给还没有攻克这个难题的jackson你们提供参考,希望你少走弯路。

参考项目:https://github.com/bigbeef/cppba-jackson
开源地址:https://github.com/bigbeef
个人博客:http://blog.cppba.com

maven的pom.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<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>com.cppba</groupId>
<artifactId>cppba-jackson</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>cppba-jackson</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--关键这里,加入jackson库-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>

编写测试代码

json字符串直接转成List,代码复杂度较高

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.cppba.jackson;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class StringToObject {
public static ObjectMapper objectMapper = new ObjectMapper();
public static void main(String[] args) throws IOException {
String jsonStr = "[{\"id\":\"1\",\"values\":[\"1\",\"2\"]},{\"id\":\"2\",\"values\":[\"11\",\"22\"]}]";
JavaType javaType = getCollectionType(ArrayList.class, JsonArrayToList.class);
List<JsonArrayToList> list = objectMapper.readValue(jsonStr, javaType);
for (JsonArrayToList jatl : list) {
System.out.println(jatl.toString());
}
}
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
}
class JsonArrayToList {
@JsonProperty("id")
private String id;
@JsonProperty("values")
private List<Integer> values;
@Override
public String toString() {
return "JsonArrayToList{" +
"id='" + id + '\'' +
", values=" + values +
'}';
}
}

json字符串转成JsonArrayToList[],代码实现比较简单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.cppba.jackson;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
public class StringToObject {
public static ObjectMapper objectMapper = new ObjectMapper();
public static void main(String[] args) throws IOException {
String jsonStr = "[{\"id\":\"1\",\"values\":[\"1\",\"2\"]},{\"id\":\"2\",\"values\":[\"11\",\"22\"]}]";
JsonArrayToList[] jsonArrayToList = objectMapper.readValue(jsonStr, JsonArrayToList[].class);
for (JsonArrayToList jatl : jsonArrayToList) {
System.out.println(jatl.toString());
}
}
}
class JsonArrayToList {
@JsonProperty("id")
private String id;
@JsonProperty("values")
private List<Integer> values;
@Override
public String toString() {
return "JsonArrayToList{" +
"id='" + id + '\'' +
", values=" + values +
'}';
}
}

运行结果

无论你是3.1还是3.2的方法实现,运行结果都是一样,如下图:

https://github.com/bigbeef

推荐

其实在业务需求满足的情况下,个人还是推荐3.2的实现方式,代码简单,容易理解!