저지에 emplty리스트와 single element리스트가 직렬화되어있는 문제가 있기 때문에. JAXBContextResolver 클래스를 추가하여 문제를 해결하려고했습니다. 목표는 모든 경우에 JSON 배열을 Android 애플리케이션에 포함하는 JSON 객체를 반환하는 것입니다 (0 요소 또는 1 요소 또는 1 이상을 반환하는 경우). 하지만 내 JSON 데이터는 동일하게 보이지 않습니다. 그리고 내 안드로이드 응용 프로그램에 오류가 발생했습니다. Expected BEGIN_OBJECT but was BEGIN_ARRAY
큰 도움을 주시면 감사하겠습니다. 내가 builder.rootUnwrapping(true);
Jersey는 빈리스트와 단일 원소리스트를 배열로 serialize합니다.
[
{
"assignedTo": "assignee1",
"businessKey": "Key1",
"createdBy": "createdBy1",
"description": "description1"
}
]
를 설정할 때 미리 감사는
@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 데이터
는 다음과 같이하지만 안드로이드 측면에서 내 문제를 해결하기 위해 다음과 같이 할 것을 권장합니다{
"project": [
{
"assignedTo": "assignee1",
"businessKey": "Key1",
"createdBy": "createdBy1",
"description": "description1"
}
]
}
내 프로젝트 클래스에 @JsonRootName(value = "project")
을 추가하여 문제를 해결했지만이 오류가 발생하지만 도움이 필요하시면 khow로 해결하지 마십시오
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
}
내가 이미 시도했지만 :( – Wijden
업데이트 된 해상도를 시도 작동하지 귀하의 프로젝트 클래스의 주석을 변경하려면, @JsonRootName을 사용하여 –
이 오류가 발생했습니다 : - JsonRootName을 유형으로 확인할 수 없습니다. \t - 주석 유형에 대한 속성 값이 정의되지 않았습니다 \t JsonRootName – Wijden