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