1.在Java bean上添加注解 @JSONType(seeAlso={Dog.class, Cat.class})
@JSONType(seeAlso={Dog.class, Cat.class})
public static class Animal {
}
@JSONType(typeName = "dog")
public static class Dog extends Animal {
public String dogName;
}
@JSONType(typeName = "cat")
public static class Cat extends Animal {
public String catName;
}
2.用法
Dog dog = new Dog();
dog.dogName = "dog1001";
String text = JSON.toJSONString(dog, SerializerFeature.WriteClassName);
Dog dog2 = (Dog) JSON.parseObject(text, Animal.class);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)