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

Java中如何将Map转换为指定结构的JSON对象数组

Java实现Map转指定结构JSON数组方案

核心逻辑:先将Map<Integer, String>的键值对转换为包含id、name属性的对象集合,再将集合序列化为JSON数组即可。

方案1:使用Jackson(Spring生态默认内置,通用性最高)

方式1:基于DTO类实现(推荐,结构清晰易维护)

首先定义数据传输类:

// 可配合Lombok的@Data注解省略getter/setter、构造器代码
public class DictItem {
    private Integer id;
    private String name;

    public DictItem(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    // 序列化必须提供属性的getter方法
    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

转换代码:

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;
import java.util.stream.Collectors;

public class MapToJsonDemo {
    public static void main(String[] args) throws Exception {
        // 原始Map
        Map<Integer, String> originMap = Map.of(1, "Terrain", 2, "Tree", 3, "Building");
        
        // 1. 流转为目标对象集合
        List<DictItem> itemList = originMap.entrySet().stream()
                .map(entry -> new DictItem(entry.getKey(), entry.getValue()))
                .collect(Collectors.toList());
        
        // 2. 序列化为JSON字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonResult = objectMapper.writeValueAsString(itemList);
        System.out.println(jsonResult);
    }
}

方式2:无需定义DTO类

如果不想额外新增类,可以直接构造Map集合再序列化:

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.stream.Collectors;

public class MapToJsonDemo {
    public static void main(String[] args) throws Exception {
        Map<Integer, String> originMap = Map.of(1, "Terrain", 2, "Tree", 3, "Building");
        
        List<Map<String, Object>> itemList = originMap.entrySet().stream()
                .map(entry -> {
                    Map<String, Object> item = new HashMap<>();
                    item.put("id", entry.getKey());
                    item.put("name", entry.getValue());
                    return item;
                })
                .collect(Collectors.toList());
        
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonResult = objectMapper.writeValueAsString(itemList);
    }
}

方案2:使用Fastjson2(高性能,国内项目常用)

转换代码更简洁:

import com.alibaba.fastjson2.JSON;
import java.util.Map;
import java.util.List;
import java.util.stream.Collectors;

public class MapToJsonDemo {
    public static void main(String[] args) {
        Map<Integer, String> originMap = Map.of(1, "Terrain", 2, "Tree", 3, "Building");
        
        List<DictItem> itemList = originMap.entrySet().stream()
                .map(entry -> new DictItem(entry.getKey(), entry.getValue()))
                .collect(Collectors.toList());
        
        // 直接序列化
        String jsonResult = JSON.toJSONString(itemList);
    }
}

注意事项

  • 最终输出的JSON为标准格式,字符串类型的key会自动包裹双引号,完全符合JSON规范,结构与你需要的目标一致。
  • Spring Boot项目无需额外引入Jackson依赖,框架已内置ObjectMapper实例,可直接注入使用。

内容的提问来源于stack exchange,提问作者João Felipe Leonello

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 09:18:01