zoukankan      html  css  js  c++  java
  • Jackson

     

    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);
        }
  • 相关阅读:
    SVN错误:Attempted to lock an already-locked dir
    DecimalFormat 中的 # 与 0 的区别(中文帮助文档中翻译可能是错误的)
    Logger.getLogger和LogFactory.getLog的区别
    在做excel导出时如何将excel直接写在输出流中
    10 -- 深入使用Spring -- 5... 实现任务的自动调度
    8 -- 深入使用Spring -- 8...2 管理Hibernate的SessionFactory
    8 -- 深入使用Spring -- 8...1 Spring提供的DAO支持
    8 -- 深入使用Spring -- 8... Spring整合Hibernate
    8 -- 深入使用Spring -- 7...4 使用自动装配
    8 -- 深入使用Spring -- 7...3 让Spring管理控制器
  • 原文地址:https://www.cnblogs.com/mr-yang-localhost/p/7207561.html
Copyright © 2011-2022 走看看