一般来说,最好将 JSON 结构准确映射到 Java。在你的情况下,你可以使用类似的东西:
public class Order {
public String name;
public ItemList items;
}
public class ItemList {
public List<Item> elements;
// and any properties you might want...
}
或者,您也可以使用(相对)新@JsonFormat注释:
public class Order {
public String name;
public ItemList items;
}
// annotation can be used on propery or class
@JsonFormat(shape=Shape.OBJECT) // instead of Shape.ARRAY
public class ItemList extends ArrayList<Item>
{
public Iterator<Item> getElements() { return this.iterator(); }
public String getSomeAttribute() { ... }
}
您在哪里强制List或被Collection序列化,就好像它是 POJO,而不是正常的特殊处理。可能会有一些副作用,因为自省用于查找可能的访问器,但一般方法应该有效