将json反序列化为pojo

8yparm6h  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(237)

我有一个测试json对象,需要反序列化到pojo中

{
  "entity": {
    "aclWrite": [
      {
        "entityId": "0-0"
      }
    ],
    "aclRead": [],
    "publicWrite": false,
    "publicRead": true,
    "birthdate": -382924800000,
    "address": "1793 Britton Rd",
    "nickname": "Karen",
    "email": "eo'neill27@ma1l2u.org"
  }
}

将保存此数据的pojo:

@XStreamAlias("entity")
public class EntityDTO {
  private String entityType;
  private String entityId;
  @XStreamImplicit(itemFieldName = "aclRead")
  private List<EntityStub> aclRead;
  @XStreamImplicit(itemFieldName = "aclWrite")
  private List<EntityStub> aclWrite;
  private Boolean publicRead;
  private Boolean publicWrite;
  @XStreamImplicit(itemFieldName = "properties")
  private Map<String, Comparable> properties;
}

xstream注解没有Mappojo的json字段应该全部注入到“属性”Map中:生日、地址、昵称和电子邮件
我有一个简单的反序列化程序:

public static <T> T deserialize(String json, Class<T> tClass) {
 if(Objects.isNull(json)) {
   return null;
 }
 XStream xstream = new XStream(new JettisonMappedXmlDriver());
 xstream.processAnnotations(tClass);
 T model = (T) xstream.fromXML(json);
 return model;
}

用法如下: EntityDTO entity = deserialize(theJSONString, EntityDTO.class); 但问题是xstream正在抱怨 No such field 对于预期的出生日期、地址、昵称和电子邮件,那么,如何配置xstream将未Map的字段自动注入到“属性”Map中呢?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题