前言
Jackson 是一个 Java 用来处理 JSON 格式数据的类库,性能非常好。
参考项目:https://github.com/bigbeef/cppba-jackson
开源地址:https://github.com/bigbeef
个人博客:http://blog.cppba.com
我也一直在用jackson,但是在开发过程中也遇到过一些需要花较长时间攻克的难题,我想把这些难题一一记录下来,给还没有攻克这个难题的jackson你们提供参考,希望你少走弯路。
1.maven的pom.xml配置
<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>
2.编写测试代码
2.1 json字符串直接转成List<JsonArrayToList >,代码复杂度较高
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 +
'}';
}
}
2.2json字符串转成JsonArrayToList[],代码实现比较简单
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.运行结果
无论你是2.1还是2.2的方法实现,运行结果都是一样,如下图:
![](https://img.haomeiwen.com/i2432809/d18d85edc7a59fd8.png)
4:推荐
其实在业务需求满足的情况下,个人还是推荐2.2的实现方式,代码简单,容易理解!
网友评论