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]