添加 jar 包支持
jackson-annotations-2.0.5.jar
jackson-core-2.0.5.jar
jackson-databind-2.0.5.jar
编写工具类
import java.io.IOException;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
public final class JsonUtils {
private static ObjectMapper mapper = new ObjectMapper();
public static String toJson(Object data) throws JsonProcessingException {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(data);
}
public static <T> T toObject(String data, Class<T> clazz) throws IOException {
return mapper.readValue(data, clazz);
}
public static <T> List<T> toList(String data, Class<T> clazz) throws IOException {
JavaType beanListType = mapper.getTypeFactory().constructParametricType(List.class, clazz);
return mapper.readValue(data, beanListType);
}
}
测试代码
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import model.Person;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import util.JsonUtils;
public class JsonTest {
/**
* Object ---> JSON
* @throws JsonProcessingException
*/
@Test
public void testObjectToJson() throws JsonProcessingException {
Person p = new Person();
p.setName("张三");
p.setAge("18");
p.setAddress("郑州");
String result = JsonUtils.toJson(p);
System.out.println("=== Object ---> JSON ===");
System.out.println(result);
}
/**
* JSON ---> Object
* @throws IOException
*/
@Test
public void testJsonToObject() throws IOException {
String data = "{\"address\":\"郑州\",\"age\":\"18\",\"name\":\"张三\"}";
Person p = JsonUtils.toObject(data, Person.class);
System.out.println("=== JSON ---> Object ===");
System.out.println(p);
System.out.println();
}
/**
* List ---> JSON
* @throws JsonProcessingException
*/
@Test
public void testListToJson() throws JsonProcessingException {
Person p1 = new Person();
p1.setName("张三");
p1.setAge("18");
p1.setAddress("郑州");
Person p2 = new Person();
p2.setName("李四");
p2.setAge("20");
p2.setAddress("北京");
List<Person> list = new ArrayList<Person>();
list.add(p1);
list.add(p2);
String result = JsonUtils.toJson(list);
System.out.println("=== List ---> JSON ===");
System.out.println(result);
System.out.println();
}
/**
* JSON ---> List
* @throws IOException
*/
@Test
public void testJsonToList() throws IOException {
String data = "[{\"address\":\"郑州\",\"age\":\"18\",\"name\":\"张三\"},{\"address\":\"北京\",\"age\":\"20\",\"name\":\"李四\"}]";
List<Person> list = JsonUtils.toList(data, Person.class);
System.out.println("=== JSON ---> List ===");
for (Person person : list) {
System.out.println(person);
}
}
}
运行结果
=== JSON ---> Object ===
Person [name=张三, age=18, address=郑州]
=== List ---> JSON ===
[ {
"name" : "张三",
"age" : "18",
"address" : "郑州"
}, {
"name" : "李四",
"age" : "20",
"address" : "北京"
} ]
=== JSON ---> List ===
Person [name=张三, age=18, address=郑州]
Person [name=李四, age=20, address=北京]
=== Object ---> JSON ===
{
"name" : "张三",
"age" : "18",
"address" : "郑州"
}