Java Jackson注释便于JSON反序列化

Java Jackson注释便于JSON反序列化,java,json,serialization,jackson,Java,Json,Serialization,Jackson,我有一个JSON文件,如下所示: { "root": [ { "id": "abc1", "x": "x1", "y": "y1" }, { "id": "abc2", "x": "x2", "y": "y2"

我有一个JSON文件,如下所示:

{
    "root": [
              {
                "id": "abc1",
                "x": "x1",
                "y": "y1"
              },
              {
                "id": "abc2",
                "x": "x2",
                "y": "y2"
              },
       ...
            ]
}
Class A {
    Map<String, String> x;
    Map<String, String> y;
}
public class A {
    private Map<String, String> x;
    private Map<String, String> y;

    @JsonCreator
    public A(@JsonProperty("root") List<Entity> entities) {
        for (Entity entity : entities) {
            x.put(entity.getId, entity.getX());
            y.put(entity.getId, entity.getY());
        }
    }

    public static class Entity {
        private String id;
        private String x;
        private String y;

        public String getId() {
            return id;
        }

        public String setId(String id) {
            this.id = id;
        }

        public String getX() {
            return x;
        }

        public String setX(String x) {
            this.x = x;
        }

        public String getY() {
            return y;
        }

        public String setY(String y) {
            this.y = y;
        }
    }
}
我想用Jackson将其反序列化为类中的对象,如下所示:

{
    "root": [
              {
                "id": "abc1",
                "x": "x1",
                "y": "y1"
              },
              {
                "id": "abc2",
                "x": "x2",
                "y": "y2"
              },
       ...
            ]
}
Class A {
    Map<String, String> x;
    Map<String, String> y;
}
public class A {
    private Map<String, String> x;
    private Map<String, String> y;

    @JsonCreator
    public A(@JsonProperty("root") List<Entity> entities) {
        for (Entity entity : entities) {
            x.put(entity.getId, entity.getX());
            y.put(entity.getId, entity.getY());
        }
    }

    public static class Entity {
        private String id;
        private String x;
        private String y;

        public String getId() {
            return id;
        }

        public String setId(String id) {
            this.id = id;
        }

        public String getX() {
            return x;
        }

        public String setX(String x) {
            this.x = x;
        }

        public String getY() {
            return y;
        }

        public String setY(String y) {
            this.y = y;
        }
    }
}
A类{
地图x;
地图y;
}
最后,我希望对象内部的贴图看起来像:

x={(abc1,x1),(abc2,x2)}, y={(abc1,y1),(abc2,y2)}


有没有办法用Jackson注释这个类,这样我就可以在一行中进行反序列化?如果不改变a类的设计,我无法找到一种方法来实现这一点。

理想情况下,X和Y将被分成两个具有ID的对象。如果可以重组JSON或Java对象以更好地满足您的需求,那么您将遵循最佳实践,代码也将更容易遵循——我知道这并不总是一个选项

可以做的一件事是创建一个自定义类,该类模仿数组中对象的结构。然后,可以对构造函数进行注释,并传入此自定义对象的列表。大概是这样的:

{
    "root": [
              {
                "id": "abc1",
                "x": "x1",
                "y": "y1"
              },
              {
                "id": "abc2",
                "x": "x2",
                "y": "y2"
              },
       ...
            ]
}
Class A {
    Map<String, String> x;
    Map<String, String> y;
}
public class A {
    private Map<String, String> x;
    private Map<String, String> y;

    @JsonCreator
    public A(@JsonProperty("root") List<Entity> entities) {
        for (Entity entity : entities) {
            x.put(entity.getId, entity.getX());
            y.put(entity.getId, entity.getY());
        }
    }

    public static class Entity {
        private String id;
        private String x;
        private String y;

        public String getId() {
            return id;
        }

        public String setId(String id) {
            this.id = id;
        }

        public String getX() {
            return x;
        }

        public String setX(String x) {
            this.x = x;
        }

        public String getY() {
            return y;
        }

        public String setY(String y) {
            this.y = y;
        }
    }
}
公共A类{
私人地图x;
私人地图;
@JsonCreator
公共A(@JsonProperty(“根”)列表实体){
for(实体:实体){
x、 put(entity.getId,entity.getX());
y、 put(entity.getId,entity.getY());
}
}
公共静态类实体{
私有字符串id;
私有字符串x;
私有字符串y;
公共字符串getId(){
返回id;
}
公共字符串集合id(字符串id){
this.id=id;
}
公共字符串getX(){
返回x;
}
公共字符串setX(字符串x){
这个.x=x;
}
公共字符串getY(){
返回y;
}
公共字符串setY(字符串y){
这个。y=y;
}
}
}

我不知道你说的“一行反序列化”是什么意思。如果您正在寻找一个更简单的替代方案,那么唯一能够满足您需求的是一个更复杂的自定义反序列化程序。

不,json不能简单地映射到该java类List@Hannes我该怎么做呢?我想,用你想要的方式解析这个
JSON
是不可能的。你的数组总是包含两个元素,或者它可以包含3、4、5等等?您必须将它解析为您的
JSON
适合的
POJO
,然后您应该将它转换为类
A
的实例。