码迷,mamicode.com
首页 > 其他好文 > 详细

Jackson

时间:2017-07-19 22:06:01      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:div   get   tdd   user   value   tun   demo1   dem   read   

技术分享 

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":"王五是个好学生"}]}}

 

Jackson

标签:div   get   tdd   user   value   tun   demo1   dem   read   

原文地址:http://www.cnblogs.com/mr-yang-localhost/p/7207561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!