眼眸繁星
您可以从Jackson库中使用JsonTypeInfo和JsonSubTypes注释。它们是句柄多态类型处理:@JsonTypeInfo用于指示序列化中包含哪些类型信息的详细信息@JsonSubTypes用于表示注解类型的子类型@JsonTypeName用于定义用于注释类的逻辑类型名称您的示例适合此解决方案,除了看起来更像简单POJO类的根对象。在您的情况下,我们应该创建有助于使用以下 3 种类型的类型结构:string, date, map:@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")@JsonSubTypes({ @JsonSubTypes.Type(value = StringValue.class, name = "string"), @JsonSubTypes.Type(value = DateValue.class, name = "date"), @JsonSubTypes.Type(value = MapValue.class, name = "map")})abstract class HasValue<T> { protected T value; public HasValue() { this(null); } public HasValue(T value) { this.value = value; } public T getValue() { return value; } public void setValue(T value) { this.value = value; } @Override public String toString() { return getClass().getSimpleName() + "{" + "value=" + value + "}"; }}class StringValue extends HasValue<String> { public StringValue() { this(null); } public StringValue(String value) { super(value); }}class DateValue extends HasValue<String> { public DateValue(String value) { super(value); } public DateValue() { this(null); }}class MapValue extends HasValue<Map<String, HasValue>> { public MapValue(Map<String, HasValue> value) { super(value); } public MapValue() { this(new LinkedHashMap<>()); } public void put(String key, HasValue hasValue) { this.value.put(key, hasValue); }}现在,我们需要引入POJO根值。它可能如下所示,但您可以根据需要添加 getter/setter。对于这个例子,下面的代码就足够了:class Root { public HasValue firstObject; public HasValue secondObject; public HasValue thirdObject; public HasValue fourthObject; @Override public String toString() { return "Root{" + "firstObject=" + firstObject + ", secondObject=" + secondObject + ", thirdObject=" + thirdObject + ", fourthObject=" + fourthObject + '}'; }}现在,我们终于可以尝试序列化和反序列化这些对象了:MapValue customerName = new MapValue();customerName.put("customerName", new StringValue("customerValue"));MapValue innerMap = new MapValue();innerMap.put("anotherObj1", new StringValue("TEST"));innerMap.put("anotherObj2", new DateValue("01/12/2018"));innerMap.put("anotherObj3", new DateValue("31/01/2018"));MapValue fourthObject = new MapValue();fourthObject.put("firstObject", innerMap);Root root = new Root();root.firstObject = new StringValue("productValue");root.secondObject = new StringValue("statusValue");root.thirdObject = customerName;root.fourthObject = fourthObject;ObjectMapper mapper = new ObjectMapper();String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);System.out.println(json);System.out.println(mapper.readValue(json, Root.class));Aboce 代码打印JSON:{ "firstObject" : { "type" : "string", "value" : "productValue" }, "secondObject" : { "type" : "string", "value" : "statusValue" }, "thirdObject" : { "type" : "map", "value" : { "customerName" : { "type" : "string", "value" : "customerValue" } } }, "fourthObject" : { "type" : "map", "value" : { "firstObject" : { "type" : "map", "value" : { "anotherObj1" : { "type" : "string", "value" : "TEST" }, "anotherObj2" : { "type" : "date", "value" : "01/12/2018" }, "anotherObj3" : { "type" : "date", "value" : "31/01/2018" } } } } }}和toString表示:Root{firstObject=StringValue{value=productValue}, secondObject=StringValue{value=statusValue}, thirdObject=MapValue{value={customerName=StringValue{value=customerValue}}}, fourthObject=MapValue{value={firstObject=MapValue{value={anotherObj1=StringValue{value=TEST}, anotherObj2=DateValue{value=01/12/2018}, anotherObj3=DateValue{value=31/01/2018}}}}}}您可以通过添加/删除任何类型的HasValue实例轻松地操作输出。