json test

    public static void main(String[] args) {
        class B {
            String name;

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            @Override
            public String toString() {
                return "B [name=" + name + "]";
            }
            
        }
        class A {
            String id;
            B b;
            public String getId() {
                return id;
            }
            public void setId(String id) {
                this.id = id;
            }
            public B getB() {
                return b;
            }
            public void setB(B b) {
                this.b = b;
            }
            @Override
            public String toString() {
                return "A [id=" + id + ", b=" + b + "]";
            }
        }
        String a = "{\"id\":\"1\",\"b\":{}}";
        A r = JSON.parseObject(a, A.class);
        System.out.println(r);
    }

输出

A [id=1, b=B [name=null]]

 

 

    public static void main(String[] args) {
        class B {
            String name;

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            @Override
            public String toString() {
                return "B [name=" + name + "]";
            }
            
        }
        class A {
            String id;
            B b;
            public String getId() {
                return id;
            }
            public void setId(String id) {
                this.id = id;
            }
            public B getB() {
                return b;
            }
            public void setB(B b) {
                this.b = b;
            }
            @Override
            public String toString() {
                return "A [id=" + id + ", b=" + b + "]";
            }
        }
        String a = "{\"id\":\"1\",\"b\":null}";
        A r = JSON.parseObject(a, A.class);
        System.out.println(r);
    }

输出

A [id=1, b=null]

posted @ 2017-12-07 22:42  raindream  阅读(203)  评论(0编辑  收藏  举报