我无法反序列化JSON对象。该对象包含一个集合,该集合被反序列化为Map,这是默认值。我需要它反序列化为Set。我的代码如下:
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。如何使用JsonDeserializer来执行此操作?我假设它在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中编写类似的东西吗?
答案 0 :(得分:1)
您的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}]}