Java Jackson 列表反序列化。嵌套列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19580856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson list deserialization. nested Lists
提问by Josh Wilson
I'm working on creating an API that has nested lists. Hymanson seems like a great tool to create objects, but I can't quite figure out how to nest a list, and I'm wondering if its possible.
我正在创建一个具有嵌套列表的 API。Hymanson 似乎是一个很好的创建对象的工具,但我不太清楚如何嵌套列表,我想知道它是否可能。
My object looks like this.
我的对象看起来像这样。
public class Order {
public String name;
public List<Item> items;
}
I'm hoping there is a way to map it to json that looks something like:
我希望有一种方法可以将它映射到 json,看起来像:
{
name : "A name"
items : {
elements : [{
price : 30
}]
}
}
We want to be able to do this so we can add properties to lists.
我们希望能够做到这一点,以便我们可以向列表添加属性。
采纳答案by Micha? Ziober
You can write custom deserializer for List<Item> items
. See below example:
您可以为List<Item> items
. 见下面的例子:
class ItemsJsonDeserializer extends JsonDeserializer<List<Item>> {
@Override
public List<Item> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
InnerItems innerItems = jp.readValueAs(InnerItems.class);
return innerItems.elements;
}
private static class InnerItems {
public List<Item> elements;
}
}
Now, you have to inform Hymanson to use it for your property. You can do this in this way:
现在,您必须通知Hyman逊将其用于您的财产。您可以通过以下方式执行此操作:
public class Order {
public String name;
@JsonDeserialize(using = ItemsJsonDeserializer.class)
public List<Item> items;
}
回答by vanza
Your JSON translates to: "the object named items
is of a type that has a property named elements
which is a list of some sort".
您的 JSON 转换为:“命名的对象items
属于一种类型,该类型具有名为elements
某种列表的属性”。
So your Item class just needs an elements
property:
所以你的 Item 类只需要一个elements
属性:
class Item {
List<Something> getElements();
}
Note that your Java code doesn't map to your JSON. Your Java classes would map to something like:
请注意,您的 Java 代码不会映射到您的 JSON。您的 Java 类将映射到类似的内容:
{
"name" : "foo",
"items" : [
{ /* encoded version of Item */ }
]
}
回答by StaxMan
In general it is best to map JSON structure exactly to Java. In your case you could use something like:
一般来说,最好将 JSON 结构完全映射到 Java。在您的情况下,您可以使用以下内容:
public class Order {
public String name;
public ItemList items;
}
public class ItemList {
public List<Item> elements;
// and any properties you might want...
}
alternatively, you could probably also use (relatively) new @JsonFormat
annotation:
或者,您也可以使用(相对)新@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() { ... }
}
where you are forcing List
or Collection
to be serialized as if it was POJO, instead of normal special handling. There may be some side-effects, since introspection is used to find possible accessors, but the general approach should work
您正在强制List
或Collection
将其序列化为 POJO,而不是正常的特殊处理。可能会有一些副作用,因为自省用于查找可能的访问器,但一般方法应该有效