个人觉得fastjson最好用,据说性能也不错
pom.xml
<?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>com.example</groupId>
<artifactId>json</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.9</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
代码
package com.example;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class TestJson {
private static Log logger = LogFactory.getLog(TestJson.class);// 定义日志工具
private static String json = "[{\"id\":1,\"name\":\"张三\",\"age\":30},{\"id\":2,\"name\":\"张三丰\",\"age\":30},{\"id\":3,\"name\":\"张丰\",\"age\":3}]";
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
logger.info("jackson");
jackson();
logger.info("fastJson");
fastJson();
logger.info("gson");
gson();
}
// jackson把json转List
private static void jackson() throws JsonParseException, JsonMappingException, IOException {
User users[] = new ObjectMapper().readValue(json, User[].class);
List<User> list = Arrays.asList(users);// 把数组转成list
printUser(list);
}
// fastJson把json转List
private static void fastJson() {
List<User> users = JSONArray.parseArray(json, User.class);
printUser(users);
}
// gson把json转List
private static void gson() {
List<User> users = new Gson().fromJson(json, new TypeToken<List<User>>() {
}.getType());
printUser(users);
}
private static void printUser(List<User> users) {
for (User user : users) {
logger.info(user);
}
}
}
User.java
package com.example;
public class User {
private Integer id;
private Integer age;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", age=" + age + ", name=" + name + "]";
}
}