我在反序列化JSON对象时遇到了麻烦。对象包含一个被反序列化为Map的集合,这是默认值。我需要它反序列化成一个集合。我的代码如下:
TaskDetail.java
@ManyToMany
private Set<RoleDetail> roleDetails = new HashSet<RoleDetail>();
public String toJson() {
return new JSONSerializer().exclude("*.class").include("roleDetails").serialize(this);
}
RoleDetail.java
@ElementCollection
@Enumerated(EnumType.STRING)
private Set<RoleFunction> roleFunctions = new HashSet<RoleFunction>();
public String toJson() {
return new JSONSerializer().exclude("*.class").include("roleFunctions").serialize(this);
}
从前端,我提交来自我的表单的数据,这些数据由我的控制器以以下格式接收:
{"name":"Clean Shelves","description":"Clean all shelves in the store","roleDetails":{"description":"A person that counts stock","id":1,"name":"Stock Counter","version":0}}
我需要将roleDetails对象反序列化为HashSet。我如何使用json反序列化器来做到这一点?我假设在RoleDetail.java:
中是这样的public static RoleDetail fromJsonToRoleDetail(String json) {
return new JSONDeserializer<RoleDetail>().use(null, RoleDetail.class).use("roleDetail.values", HashSet.class).deserialize(json);
}
或者我也必须在TaskDetail.java中编写类似的代码吗?
您的roleDetails应该是一个对象数组,而不是一个对象。
你应该有这样的东西:
{"name":"Clean Shelves","description":"Clean all shelves in the store","roleDetails":[{"description":"A person that counts stock","id":1,"name":"Stock Counter","version":0}]}