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

Java中如何将一个JSON数组赋值给另一个JSON数组并解析?

Java中解析嵌套JSON数组的实现方法

当然可以实现!在Java里处理JSON数据,最常用的就是Jackson、Gson这两个主流库,我给你分别演示两种实现方式,你可以根据自己的项目依赖情况选合适的。

一、使用Jackson库实现

Jackson是Spring生态里默认的JSON处理库,用起来很顺手。

1. 先引入依赖(Maven为例)

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version> <!-- 用最新稳定版即可 -->
</dependency>

2. 定义实体类(对应JSON里的对象结构)

先创建一个简单的实体类来映射JSON中的{"label":"xxx", "value":"xxx"}对象:

public class ColorItem {
    private String label;
    private String value;

    // 必须要有无参构造方法(Jackson反射实例化需要)
    public ColorItem() {}

    // getter和setter方法
    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    // 可选:重写toString方便调试查看结果
    @Override
    public String toString() {
        return "ColorItem{" +
                "label='" + label + '\'' +
                ", value='" + value + '\'' +
                '}';
    }
}

3. 解析JSON数组的代码

把你的JSON字符串赋值给变量,然后用Jackson的ObjectMapper来解析:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;

public class JsonParseDemo {
    public static void main(String[] args) {
        // 原JSON字符串
        String jsonStr = "[ [ { \"label\":\"Red\", \"value\":\"8\" }, { \"label\":\"Yellow\", \"value\":\"9\" }, { \"label\":\"Pink\", \"value\":\"10\" } ] ]";

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // 因为是嵌套数组,用TypeReference来指定泛型类型
            List<List<ColorItem>> nestedColorList = objectMapper.readValue(jsonStr, new TypeReference<List<List<ColorItem>>>() {});

            // 遍历输出验证解析结果
            for (List<ColorItem> colorList : nestedColorList) {
                for (ColorItem item : colorList) {
                    System.out.println(item);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

二、使用Gson库实现

Gson是Google出品的JSON库,语法更简洁,也很受欢迎。

1. 引入依赖(Maven为例)

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version> <!-- 最新稳定版即可 -->
</dependency>

2. 解析代码(实体类可复用上面的ColorItem)

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.List;

public class GsonParseDemo {
    public static void main(String[] args) {
        String jsonStr = "[ [ { \"label\":\"Red\", \"value\":\"8\" }, { \"label\":\"Yellow\", \"value\":\"9\" }, { \"label\":\"Pink\", \"value\":\"10\" } ] ]";

        Gson gson = new Gson();
        // 用TypeToken来处理泛型嵌套的类型
        List<List<ColorItem>> nestedColorList = gson.fromJson(jsonStr, new TypeToken<List<List<ColorItem>>>() {}.getType());

        // 遍历验证解析结果
        for (List<ColorItem> colorList : nestedColorList) {
            for (ColorItem item : colorList) {
                System.out.println(item);
            }
        }
    }
}

三、临时场景:不用实体类,用Map解析

如果你只是临时处理这个JSON,不想专门写实体类,也可以用Map<String, String>来映射:
以Jackson为例:

List<List<Map<String, String>>> nestedMapList = objectMapper.readValue(jsonStr, new TypeReference<List<List<Map<String, String>>>>() {});
// 遍历输出
for (List<Map<String, String>> mapList : nestedMapList) {
    for (Map<String, String> map : mapList) {
        System.out.println("label: " + map.get("label") + ", value: " + map.get("value"));
    }
}

Gson的写法类似,只需把泛型换成List<List<Map<String, String>>>即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:01:29