1》包含两个对象
String testRootStr = "{"tUser":{"mId":1,"mName":"test model","mDesc":"model1 desc"},"tClass":{"dId":1,"dName":"test Demo1","dDesc":"demo1 desc"}}"; TestRoot testRoot1 = mapper.readValue(testRootStr, TestRoot.class); TestRoot testRoot2 = mapper.readValue(testRootStr.getBytes(), TestRoot.class);
Map map1 = mapper.readValue(testRootStr, Map.class);
2》包一个对象 和 一个List集合
String testRootStr = "{"tUser":{"mId":1,"mName":"test model","mDesc":"model1 desc"},"tClass":[{"dId":1,"dName":"test Demo1","dDesc":"demo1 desc"},{"dId":2,"dName":"test Demo2","dDesc":"demo2 desc"},{"dId":3,"dName":"test Demo3","dDesc":"demo3 desc"}]}"; TestRoot testRoot1 = mapper.readValue(testRootStr, TestRoot.class); TestRoot testRoot2 = mapper.readValue(testRootStr.getBytes(), TestRoot.class);
Map map1 = mapper.readValue(testRootStr, Map.class);
3》一个对象 和 一个List集合,List的对象内部有一个List集合
TestRoot testRoot = new TestRoot(){{ settClass(new ArrayList<TClass>(){{ add(new TClass(){{ setdId(1);setdName("test Demo1");setdDesc("demo1 desc"); setStudentList(new ArrayList<Student>(){{ add(new Student(){{setStuId(1);setStuName("张三");setStuMsg("张三是个好学生");}}); add(new Student(){{setStuId(2);setStuName("李四");setStuMsg("李四是个好学生");}}); add(new Student(){{setStuId(3);setStuName("王五");setStuMsg("王五是个好学生");}}); }}); }}); add(new TClass(){{setdId(2);setdName("test Demo2");setdDesc("demo2 desc"); setStudentList(new ArrayList<Student>(){{ add(new Student(){{setStuId(3);setStuName("张三");setStuMsg("张三是个好学生");}}); add(new Student(){{setStuId(4);setStuName("李四");setStuMsg("李四是个好学生");}}); add(new Student(){{setStuId(5);setStuName("王五");setStuMsg("王五是个好学生");}}); }}); }}); }}); settUser(new TUser(){{setmId(1);setmName("test model");setmDesc("model1 desc");}}); }}; String testRootStr = mapper.writeValueAsString(testRoot);
反序列化:
String testRootStr = "{"tUser":{"mId":1,"mName":"test model","mDesc":"model1 desc"},"tClass":[{"dId":1,"dName":"test Demo1","dDesc":"demo1 desc","studentList":[{"stuId":1,"stuName":"张三","stuMsg":"张三是个好学生"},{"stuId":2,"stuName":"李四","stuMsg":"李四是个好学生"},{"stuId":3,"stuName":"王五","stuMsg":"王五是个好学生"}]},{"dId":2,"dName":"test Demo2","dDesc":"demo2 desc","studentList":[{"stuId":3,"stuName":"张三","stuMsg":"张三是个好学生"},{"stuId":4,"stuName":"李四","stuMsg":"李四是个好学生"},{"stuId":5,"stuName":"王五","stuMsg":"王五是个好学生"}]}]}"; TestRoot testRoot1 = mapper.readValue(testRootStr, TestRoot.class); // Map map1 = mapper.readValue(testRootStr, Map.class); TestRoot testRoot2 = mapper.readValue(testRootStr.getBytes(), TestRoot.class); Map map = mapper.readValue(testRootStr,Map.class);
4》包含两个对象,一个对象内有属性 + List集合
{"tUser":{"mId":1,"mName":"test model","mDesc":"model1 desc"},"tClass":{"dId":1,"dName":"test Demo1","dDesc":"demo1 desc","studentList":[{"stuId":1,"stuName":"张三","stuMsg":"张三是个好学生"},{"stuId":2,"stuName":"李四","stuMsg":"李四是个好学生"},{"stuId":3,"stuName":"王五","stuMsg":"王五是个好学生"}]}}
5》list集合反序列化
String string = "[{"stuId":1,"stuName":"张三","stuMsg":"张三是个好学生"},{"stuId":2,"stuName":"李四","stuMsg":"李四是个好学生"},{"stuId":3,"stuName":"王五","stuMsg":"王五是个好学生"}]";
//反序列化 List<Demo> demoList = objectMapper.readValue(string, new TypeReference<List<Demo>>(){});
转换问题
反序列化时,json含有的字段,对象不含有,会报错,添加一下配置解决:
public static final ObjectMapper objectMapper; static { objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); }