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

如何在Java中创建含动态键的Model Object适配可变JSON结构

在Java中处理含动态键的JSON Model对象

你的JSON结构里有一个固定字段id,剩下的都是动态变化的键值对,这种场景在Java里有几种实用的处理方式,下面给你详细拆解:

方案一:使用Jackson的@JsonAnyGetter和@JsonAnySetter(推荐)

这是最优雅的面向对象方案,既能强类型管理固定字段,又能灵活兼容动态键,完全贴合你的需求:

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;

public class DynamicResponseModel {
    // 固定字段,强类型维护
    private String id;
    // 专门存储动态键值对的容器
    private Map<String, String> dynamicFields = new HashMap<>();

    // 固定字段的常规getter/setter
    public String getId() {
        return id;
    }

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

    // Jackson自动把未匹配到固定字段的键值对调用这个方法存入容器
    @JsonAnySetter
    public void setDynamicField(String key, String value) {
        dynamicFields.put(key, value);
    }

    // 序列化时,会把容器里的键值对作为顶级字段输出,和原JSON结构一致
    @JsonAnyGetter
    public Map<String, String> getDynamicFields() {
        return dynamicFields;
    }

    // 测试用例
    public static void main(String[] args) throws Exception {
        String json = "{ \"id\":\"12345\", \"What is the name of your first car\": \"a\", \"Mothers maiden name\": \"b\", \"Which city you are born in\": \"c\" }";
        ObjectMapper mapper = new ObjectMapper();
        
        // 反序列化JSON到Model
        DynamicResponseModel model = mapper.readValue(json, DynamicResponseModel.class);
        System.out.println("固定ID:" + model.getId());
        System.out.println("动态字段集合:" + model.getDynamicFields());
        
        // 序列化Model回JSON,结构和原输入完全一致
        String serializedJson = mapper.writeValueAsString(model);
        System.out.println("序列化后的JSON:" + serializedJson);
    }
}

这个方案的核心优势:

  • 固定字段强类型,避免手动类型转换的错误
  • 动态字段统一管理,方便遍历、修改等操作
  • 序列化/反序列化完全贴合原JSON结构,不会出现额外嵌套

方案二:直接使用Map<String, Object>(快速临时处理)

如果只是临时处理JSON,不需要长期维护的Model类,可以直接用Map接收,手动拆分固定字段和动态字段:

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;

public class MapQuickExample {
    public static void main(String[] args) throws Exception {
        String json = "{ \"id\":\"12345\", \"What is the name of your first car\": \"a\", \"Mothers maiden name\": \"b\", \"Which city you are born in\": \"c\" }";
        ObjectMapper mapper = new ObjectMapper();
        
        Map<String, Object> jsonMap = mapper.readValue(json, Map.class);
        String id = (String) jsonMap.get("id");
        // 移除固定字段后,剩下的就是动态键值对
        jsonMap.remove("id");
        
        System.out.println("提取的ID:" + id);
        System.out.println("动态字段:" + jsonMap);
    }
}

这种方式优点是快速上手,不需要额外编写类;缺点是缺乏类型安全,所有值都是Object类型,需要手动转换,适合简单一次性场景。

方案三:使用Jackson的ObjectNode(动态JSON操作)

如果需要对JSON结构做频繁的修改、添加字段等操作,可以用Jackson的树模型:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class ObjectNodeExample {
    public static void main(String[] args) throws Exception {
        String json = "{ \"id\":\"12345\", \"What is the name of your first car\": \"a\", \"Mothers maiden name\": \"b\", \"Which city you are born in\": \"c\" }";
        ObjectMapper mapper = new ObjectMapper();
        
        ObjectNode node = (ObjectNode) mapper.readTree(json);
        String id = node.get("id").asText();
        // 移除固定ID节点
        node.remove("id");
        
        System.out.println("提取的ID:" + id);
        System.out.println("动态字段结构:\n" + node.toPrettyString());
    }
}

这种方式适合需要灵活操作JSON节点的场景,但同样没有强类型保障,适合工具类或临时脚本场景。

总结:如果是长期维护的业务代码,优先选方案一;临时处理或简单场景可以用方案二/三。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:45:27