1
我使用JAXB标注序列化与Spring /反序列化对象。使用JAXB注释时,如何解开了JSON根?
这是我的响应等级:
@XmlRootElement(name = "animals")
public class PetClinic implements Serializable {
@XmlElementWrapper(name = "animals")
@XmlElement(name = "animal")
private Collection<Animal> animals;
public Collection<Animal> getAnimals() {
return animals;
}
public void setAnimalsCollection<Animal> animals) {
this.animals = animals;
}
}
我想这样
{
"animals": [
{...},
{...}
]
}
一个JSON我试图解开根元素,但我所有的解决方案没有奏效:
1)我试图使用@JsonUnwrapped注释上字段
2)我试图使用@Json序列化
@JsonSerialize(using = PetClinicSerializer.class)
串行代码:
ObjectMapper mapper = new ObjectMapper();
@Override
public void serialize(PetClinic clinic, JsonGenerator generator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
String clinicAsString = mapper.writeValueAsString(clinic);
generator.writeString(clinicAsString);
}
我怎样才能得到一个JSON上面提到的?
和你现在得到些什么? – araknoid