jackSon解析JSON,SpringMVC内置的解析器就是这个
package cn.itcast.test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class jackSonTest { @Test public void test1() throws IOException { // 对象嵌套数组嵌套对象 String json1 = "{\"id\":1,\"name\":\"JAVAEE-1703\",\"stus\":[{\"id\":101,\"name\":\"刘一\",\"age\":16}]}"; // 数组 String json2 = "[\"北京\",\"天津\",\"杭州\"]"; ObjectMapper mapper = new ObjectMapper(); Grade grade = mapper.readValue(json1, Grade.class); System.out.println(grade); List<String> list = mapper.readValue(json2, new TypeReference<List<String>>() { }); System.out.println(list); } @Test public void test2() throws JsonProcessingException { ArrayList<Student> students = new ArrayList<>(); for (int i = 0; i < 3; i++) { students.add(new Student(100 + i, "二稿" + i, 1000 + i)); } Grade grade = new Grade(22, "语文", students); // System.out.println(grade); ObjectMapper objectMapper = new ObjectMapper(); String s = objectMapper.writeValueAsString(grade); System.out.println(s); } }除了JSON官方的对类没有要求,剩下的都要求是标准的类,否则无法解析,因为都用到了反射。