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

如何将指定JSON转换为Java POJO类?优化类结构需求咨询

解决方案:用Map统一存储数字键对应的列表

嘿,我来帮你搞定这个问题!你想要把JSON里那些数字键("0"、"1"、"2")对应的List<Request>统一放到一个集合里,用数字键作为索引,而不是一个个定义字段对吧?这其实用Jackson的Map<Integer, List<Request>>就能轻松实现,不需要再写一堆@JsonProperty注解了。

完整的POJO实现

这里假设你用了Lombok的@Data注解来简化代码,如果没用的话,自己补上getter/setter即可:

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.List;
import java.util.Map;

@Data
public class Root {
    // 用Map<Integer, List<Request>>接收所有数字键对应的列表
    // Jackson会自动把JSON中的字符串键(比如"0")转换成Integer类型
    private Map<Integer, List<Request>> requests;

    @Data
    public static class Request { // 改成static内部类,否则Jackson无法正常实例化非静态内部类
        private int id;
        private String title;
        private int level;
        private List<Request> children;
        @JsonProperty("parent_id")
        private Integer parentId; // 改成Integer,因为JSON里有null值,基本类型int无法存储null
    }
}

关键说明

  • Map的核心作用:你的JSON本质是键值对结构,用Map<Integer, List<Request>>可以直接接收所有数字键对应的列表,Jackson会自动完成字符串键到Integer的转换,完全不需要手动为每个键添加@JsonProperty。
  • Request类的必要调整:把parentId的类型从int改成Integer,因为JSON里存在"parent_id": null的情况,基本类型int无法存储null值,会导致反序列化失败。另外内部类必须改成static,否则Jackson无法实例化非静态内部类。
  • 便捷访问方式:你可以通过root.getRequests().get(0)获取原来"0"对应的列表,root.getRequests().get(1)获取"1"对应的列表,以此类推,完全符合你用数字键作为索引的需求。

可选:转换成索引连续的List

如果希望得到一个索引连续的List<List<Request>>(比如即使JSON里缺失某个数字键,也能返回空列表),可以在Root类里添加一个辅助方法:

import java.util.ArrayList;
import java.util.List;

public List<List<Request>> getAsIndexedList() {
    if (requests == null) {
        return List.of();
    }
    // 找到最大的索引值
    int maxIndex = requests.keySet().stream().max(Integer::compareTo).orElse(-1);
    List<List<Request>> indexedList = new ArrayList<>(maxIndex + 1);
    // 填充每个索引的列表,不存在的索引用空列表兜底
    for (int i = 0; i <= maxIndex; i++) {
        indexedList.add(requests.getOrDefault(i, List.of()));
    }
    return indexedList;
}

这样你就可以像访问普通列表一样使用root.getAsIndexedList().get(0)来获取对应索引的内容了。

测试示例

用Jackson的ObjectMapper来反序列化你的目标JSON,验证效果:

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException {
        String json = "{\n" +
                "    \"0\": [\n" +
                "        { \"id\": 12123, \"title\": \"sfsdf\", \"level\": 0, \"children\": [], \"parent_id\": null }\n" +
                "    ],\n" +
                "    \"1\": [\n" +
                "        { \"id\": 213, \"title\": \"d\", \"level\": 1, \"children\": [], \"parent_id\": 1212312 },\n" +
                "        { \"id\": 3232, \"title\": \"dfsdf\", \"level\": 1, \"children\": [], \"parent_id\": 42 },\n" +
                "        { \"id\": 234, \"title\": \"tder\", \"level\": 1, \"children\": [], \"parent_id\": 122 }\n" +
                "    ],\n" +
                "    \"2\": [\n" +
                "        { \"id\": 452, \"title\": \"Blll\", \"level\": 2, \"children\": [], \"parent_id\": 322 },\n" +
                "        { \"id\": 123, \"title\": \"trrr\", \"level\": 11, \"children\": [], \"parent_id\": 1221 },\n" +
                "        { \"id\": 33, \"title\": \"sdfw\", \"level\": 2123, \"children\": [], \"parent_id\": 10 }\n" +
                "    ]\n" +
                "}";

        ObjectMapper objectMapper = new ObjectMapper();
        Root root = objectMapper.readValue(json, Root.class);

        // 测试访问
        System.out.println("索引0的第一个请求标题:" + root.getRequests().get(0).get(0).getTitle());
        System.out.println("索引1的请求数量:" + root.getRequests().get(1).size());
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:41:39