由于在泽西岛有一个带有序列化的空列表和单元素列表的问题。我试图通过添加 JAXBContextResolver 类来修复它。目标是在所有情况下都将包含 JSON 数组的 JSON 对象返回到我的 Android 应用程序(如果它返回 0 个元素或 1 个元素或超过 1 个)。但我的 JSON 数据看起来不一样。我的 android 应用程序出现错误,Expected BEGIN_OBJECT but was BEGIN_ARRAY
非常感谢您的帮助。提前致谢
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
private final Set<Class> types;
// pojo class
private Class[] ctypes = { Workitem.class, Project.class, User.class };
public JAXBContextResolver() throws Exception {
NaturalBuilder builder = JSONConfiguration.natural();
//assure the rootelement name appears in the json structure
builder.rootUnwrapping(false);
this.types = new HashSet(Arrays.asList(ctypes));
// json configuration
this.context = new JSONJAXBContext(builder.build(), ctypes);
}
@Override
public JAXBContext getContext(Class<?> objectType) {
return (types.contains(objectType)) ? context : null;
}
}
当我设置时,我的 JSON 数据看起来像这样builder.rootUnwrapping(true);
[
{
"assignedTo": "assignee1",
"businessKey": "Key1",
"createdBy": "createdBy1",
"description": "description1"
}
]
但我希望它是这样来解决我在 Android 方面的问题:
{
"project": [
{
"assignedTo": "assignee1",
"businessKey": "Key1",
"createdBy": "createdBy1",
"description": "description1"
}
]
}
我添加@JsonRootName(value = "project")
到我的项目类以解决我的问题但我收到此错误但我不知道如何解决它请我需要帮助
Multiple markers at this line
- JsonRootName cannot be resolved to a type
- The attribute value is undefined for the annotation type JsonRootName
我的项目类是这样的:
@XmlRootElement
@JsonRootName(value = "project")
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
private String description;
private String businessKey;
private String createdBy;
private String assignedTo;
public Project() {
}
// getter and setter method
}