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);
        }
  • 相关阅读:
    node的function函数和路由代码的小例子
    关于node回调函数中同步和异步操作的理解
    node初学
    CTF知识点总结(二)
    知识图谱
    论文笔记 无监督与混合IDS
    CTF知识点总结(一)
    论文笔记 网络安全图谱以及溯源算法
    攻防世界 wtf.sh-150
    攻防世界 Web_php_wrong_nginx_config
  • 原文地址:https://www.cnblogs.com/mr-yang-localhost/p/7207561.html
Copyright © 2011-2022 走看看