You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Jackson将嵌套JSON数组反序列化为自定义Coordinates类

刚好我之前处理过类似的Jackson反序列化场景,给你分享几个实用的解决方案,帮你把JSON里的二维坐标数组转成自定义的Coordinates类:

方案一:自定义JsonDeserializer(最灵活的方式)

这种方式适合需要复杂逻辑转换的场景,我们可以写一个专门的反序列化器来解析二维数组:

  1. 首先创建CoordinatesDeserializer类,继承Jackson的JsonDeserializer:
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.io.IOException;

public class CoordinatesDeserializer extends JsonDeserializer<Coordinates> {
    @Override
    public Coordinates deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        // 读取JSON中的二维数组节点
        ArrayNode coordinatesArray = parser.getCodec().readTree(parser);
        
        // 解析第一个坐标对 [x1, y1]
        ArrayNode firstPoint = (ArrayNode) coordinatesArray.get(0);
        double x1 = Double.parseDouble(firstPoint.get(0).asText());
        double y1 = Double.parseDouble(firstPoint.get(1).asText());
        
        // 解析第二个坐标对 [x2, y2]
        ArrayNode secondPoint = (ArrayNode) coordinatesArray.get(1);
        double x2 = Double.parseDouble(secondPoint.get(0).asText());
        double y2 = Double.parseDouble(secondPoint.get(1).asText());
        
        // 返回构造好的Coordinates对象
        return new Coordinates(x1, y1, x2, y2);
    }
}
  1. 然后在CommentFile类的coordinates字段上添加@JsonDeserialize注解,指定使用我们刚才写的反序列化器:
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.io.Serializable;

public class CommentFile implements Serializable{
    private Long id;
    private String comment;
    private String user;
    private String date;
    private String shape;
    
    // 指定自定义反序列化器
    @JsonDeserialize(using = CoordinatesDeserializer.class)
    private Coordinates coordinates;
    
    // 构造器、getter、setter方法(根据需要实现)
}

方案二:利用@JsonCreator注解(更简洁的写法)

如果你的转换逻辑比较简单,也可以直接在Coordinates类中通过构造器配合注解来完成映射,不需要单独写反序列化器:

import com.fasterxml.jackson.annotation.JsonCreator;
import java.io.Serializable;
import java.util.List;

public class Coordinates implements Serializable{
    private double x1;
    private double y1;
    private double x2;
    private double y2;

    // 用@JsonCreator标记构造器,Jackson会自动把JSON数组传入
    @JsonCreator
    public Coordinates(List<List<String>> coordinatesArray) {
        // 解析第一个坐标点
        List<String> firstPoint = coordinatesArray.get(0);
        this.x1 = Double.parseDouble(firstPoint.get(0));
        this.y1 = Double.parseDouble(firstPoint.get(1));
        
        // 解析第二个坐标点
        List<String> secondPoint = coordinatesArray.get(1);
        this.x2 = Double.parseDouble(secondPoint.get(0));
        this.y2 = Double.parseDouble(secondPoint.get(1));
    }
    
    // 也可以用数组类型作为参数,写法更直接:
    // @JsonCreator
    // public Coordinates(String[][] coordinatesArray) {
    //     this.x1 = Double.parseDouble(coordinatesArray[0][0]);
    //     this.y1 = Double.parseDouble(coordinatesArray[0][1]);
    //     this.x2 = Double.parseDouble(coordinatesArray[1][0]);
    //     this.y2 = Double.parseDouble(coordinatesArray[1][1]);
    // }
    
    // getter、setter方法(根据需要实现)
}

一些注意事项

  • 确保你的项目已经正确引入Jackson的核心依赖(比如jackson-databind);
  • 可以在代码中增加异常处理逻辑(比如判断数组长度是否为2,避免IndexOutOfBoundsException;捕获NumberFormatException处理非法数字格式);
  • 如果需要序列化回去(把Coordinates转成二维数组),可以对应写一个JsonSerializer或者用@JsonValue注解实现反向转换。

内容的提问来源于stack exchange,提问作者Suule

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:35:15