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

如何优雅地将含冒号键的JSON映射为Java对象?

优雅映射带特殊键的JSON到Java对象

你可以通过Map接收动态键的方式来优雅解决这个问题,不需要让Java属性包含冒号。下面是主流JSON库(Jackson、Gson)的实现方案:

核心思路

JSON中entries下的键是动态且包含特殊字符(冒号),直接用固定属性无法映射,因此将entries定义为Map<String, Entry>类型,其中Entry对应子对象的结构。


方案1:使用Jackson实现

1. 定义Java实体类

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.Map;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Response {
    private Map<String, Entry> entries;

    // Getter & Setter
    public Map<String, Entry> getEntries() {
        return entries;
    }

    public void setEntries(Map<String, Entry> entries) {
        this.entries = entries;
    }

    public static class Entry {
        private String name;
        private String type;

        // Getter & Setter
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }
}

2. 反序列化JSON

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

public class Main {
    public static void main(String[] args) throws IOException {
        String json = "{\n" +
                "    \"entries\": {\n" +
                "        \"prefix:someId\": {\n" +
                "            \"name\": \"prefix:someId\",\n" +
                "            \"type\": \"ID\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        ObjectMapper objectMapper = new ObjectMapper();
        Response response = objectMapper.readValue(json, Response.class);

        // 遍历获取数据
        response.getEntries().forEach((key, entry) -> {
            System.out.println("键: " + key);
            System.out.println("name: " + entry.getName());
            System.out.println("type: " + entry.getType());
        });
    }
}

方案2:使用Gson实现

1. 定义Java实体类

import java.util.Map;

public class Response {
    private Map<String, Entry> entries;

    // Getter & Setter
    public Map<String, Entry> getEntries() {
        return entries;
    }

    public void setEntries(Map<String, Entry> entries) {
        this.entries = entries;
    }

    public static class Entry {
        private String name;
        private String type;

        // Getter & Setter
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }
}

2. 反序列化JSON

import com.google.gson.Gson;

public class Main {
    public static void main(String[] args) {
        String json = "{\n" +
                "    \"entries\": {\n" +
                "        \"prefix:someId\": {\n" +
                "            \"name\": \"prefix:someId\",\n" +
                "            \"type\": \"ID\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        Gson gson = new Gson();
        Response response = gson.fromJson(json, Response.class);

        // 遍历获取数据
        response.getEntries().forEach((key, entry) -> {
            System.out.println("键: " + key);
            System.out.println("name: " + entry.getName());
            System.out.println("type: " + entry.getType());
        });
    }
}

这种方式完全符合Java编码规范,不需要处理特殊字符的属性名,同时保留了对象映射的可读性和可维护性,比JSONPath更优雅。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 20:09:30