使用Jackson 如何把json字符串反序列化为List呢(转)

使用Jackson 如何把json字符串反序列化为List呢?
(List中是自定义对象)
先看下常规的反序列化:

Java代码 复制代码 收藏代码
  1. @Test
  2. publicvoid test_reserialize(){
  3. String jsonInput = "{\"addrr\":{\"country\":\"中国\",\"state\":\"湖北省\",\"street\":\"清河\"},\"age\":25,\"hobby\":\"\",\"name\":\"黄威\"}";
  4. ObjectMapper mapper = new ObjectMapper();
  5. Student student;
  6. try {
  7. student = mapper.readValue(jsonInput, Student.class);
  8. System.out.println(student.getAddrr().getStreet());
  9. System.out.println(student.getName());
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. }
  13. }
@Test
    public void test_reserialize(){
        String jsonInput = "{\"addrr\":{\"country\":\"中国\",\"state\":\"湖北省\",\"street\":\"清河\"},\"age\":25,\"hobby\":\"\",\"name\":\"黄威\"}";
        ObjectMapper mapper = new ObjectMapper();
        Student student;
        try {
            student = mapper.readValue(jsonInput, Student.class);
            System.out.println(student.getAddrr().getStreet());
            System.out.println(student.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

运行结果:

序列化

Java代码 复制代码 收藏代码
  1. @Test
  2. publicvoid test_004(){
  3. List<Teacher> list=new ArrayList<Teacher>();
  4. Teacher t=new Teacher();
  5. t.setId(2);
  6. t.setName("雄鹰表");
  7. t.setTitle("aa");
  8. list.add(t);
  9. t=new Teacher();
  10. t.setId(3);
  11. t.setName("陈定生");
  12. t.setTitle("bb");
  13. list.add(t);
  14. t=new Teacher();
  15. t.setId(4);
  16. t.setName("张阿勇");
  17. t.setTitle("cc");
  18. list.add(t);
  19. ObjectMapper mapper = new ObjectMapper();
  20. String content;
  21. try {
  22. content = mapper.writeValueAsString(list);
  23. System.out.println(content);
  24. } catch (JsonGenerationException e) {
  25. e.printStackTrace();
  26. } catch (JsonMappingException e) {
  27. e.printStackTrace();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
@Test
    public void test_004(){
        List<Teacher> list=new ArrayList<Teacher>();
        Teacher t=new Teacher();
        t.setId(2);
        t.setName("雄鹰表");
        t.setTitle("aa");
        list.add(t);

        t=new Teacher();
        t.setId(3);
        t.setName("陈定生");
        t.setTitle("bb");
        list.add(t);

        t=new Teacher();
        t.setId(4);
        t.setName("张阿勇");
        t.setTitle("cc");
        list.add(t);

        ObjectMapper mapper = new ObjectMapper();
        String content;
        try {
            content = mapper.writeValueAsString(list);
            System.out.println(content);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

运行结果:
[{“id”:2,”title”:”aa”,”name”:”雄鹰表”},{“id”:3,”title”:”bb”,”name”:”陈定生”},{“id”:4,”title”:”cc”,”name”:”张阿勇”}]

反序列化
把上述json字符串反序列化为List
代码如下:

Java代码 复制代码 收藏代码
  1. @Test
  2. publicvoid test_006(){
  3. String jsonInput="[{\"id\":2,\"title\":\"aa\",\"name\":\"雄鹰表\"},{\"id\":3,\"title\":\"bb\",\"name\":\"陈定生\"},{\"id\":4,\"title\":\"cc\",\"name\":\"张阿勇\"}]";
  4. ObjectMapper mapper = new ObjectMapper();
  5. List student;
  6. try {
  7. student = mapper.readValue(jsonInput, List.class);
  8. System.out.println(student.size());
  9. } catch (Exception e) {
  10. e.printStackTrace();
  11. }
  12. }
@Test
    public void test_006(){
        String jsonInput="[{\"id\":2,\"title\":\"aa\",\"name\":\"雄鹰表\"},{\"id\":3,\"title\":\"bb\",\"name\":\"陈定生\"},{\"id\":4,\"title\":\"cc\",\"name\":\"张阿勇\"}]";
        ObjectMapper mapper = new ObjectMapper();
        List student;
        try {
            student = mapper.readValue(jsonInput, List.class);
            System.out.println(student.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

运行时

没有达到预期效果,虽然反序列化成了List,但是并不是List<Teacher>,而是List<HashMap>

如何解决这个问题呢?
解决方法:使用mapper.getTypeFactory().constructParametricType

Java代码 复制代码 收藏代码
  1. /**
  2. * 获取泛型的Collection Type
  3. * @param collectionClass 泛型的Collection
  4. * @param elementClasses 元素类
  5. * @return JavaType Java类型
  6. * @since 1.0
  7. */
  8. publicstatic JavaType getCollectionType(ObjectMapper mapper,Class<?> collectionClass, Class<?>... elementClasses) {
  9. return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
  10. }
  11. @Test
  12. publicvoid test_006(){
  13. String jsonInput="[{\"id\":2,\"title\":\"aa\",\"name\":\"雄鹰表\"},{\"id\":3,\"title\":\"bb\",\"name\":\"陈定生\"},{\"id\":4,\"title\":\"cc\",\"name\":\"张阿勇\"}]";
  14. ObjectMapper mapper = new ObjectMapper();
  15. List student;
  16. try {
  17. student = mapper.readValue(jsonInput, getCollectionType(mapper, List.class, Teacher.class));
  18. System.out.println(student.size());
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }
/**   
     * 获取泛型的Collection Type  
     * @param collectionClass 泛型的Collection   
     * @param elementClasses 元素类   
     * @return JavaType Java类型   
     * @since 1.0   
     */   
 public static JavaType getCollectionType(ObjectMapper mapper,Class<?> collectionClass, Class<?>... elementClasses) {   
     return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);   
 }
    @Test
    public void test_006(){
        String jsonInput="[{\"id\":2,\"title\":\"aa\",\"name\":\"雄鹰表\"},{\"id\":3,\"title\":\"bb\",\"name\":\"陈定生\"},{\"id\":4,\"title\":\"cc\",\"name\":\"张阿勇\"}]";
        ObjectMapper mapper = new ObjectMapper();
        List student;
        try {
            student = mapper.readValue(jsonInput, getCollectionType(mapper, List.class, Teacher.class));
            System.out.println(student.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

运行时:

http://blog.csdn.net/hw1287789687/article/details/46955179

扫描二维码关注公众号,回复: 794815 查看本文章

作者:黄威

联系方式:[email protected]

  • 大小: 6.5 KB
  • 大小: 96.4 KB
  • 大小: 94 KB

猜你喜欢

转载自frank1998819.iteye.com/blog/2229079