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

如何在Java中读取JSON嵌套数组内的数据(PokeApi示例)

解决PokeApi嵌套数组数据读取问题

要获取返回结果中types数组的内容,你需要根据API返回的JSON结构,补充对应的嵌套实体类,再在主类中解析并遍历数据。具体步骤如下:

1. 补充嵌套结构的实体类

根据PokeApi返回的types字段结构(每个元素包含slot和嵌套的type对象),创建两个辅助类:

Type类(对应嵌套的type对象)

package org.example;

public class Type {
    private String name;
    private String url;

    // Getter方法
    public String getName() {
        return name;
    }

    public String getUrl() {
        return url;
    }
}

PokemonType类(对应types数组的每个元素)

package org.example;

public class PokemonType {
    private int slot;
    private Type type;

    // Getter方法
    public int getSlot() {
        return slot;
    }

    public Type getType() {
        return type;
    }
}

2. 修改Transcript类,添加types数组字段

在原Transcript类中添加types列表字段,并生成对应的getter方法:

package org.example;

import java.util.List;

public class Transcript {
    private String name;
    private int id;
    private int height;
    private int order;
    private int weight;
    // 添加types数组字段
    private List<PokemonType> types;

    // 原有的getter/setter方法保留
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public int getHeight() { return height; }
    public void setHeight(int height) { this.height = height; }
    public int getOrder() { return order; }
    public void setOrder(int order) { this.order = order; }
    public int getWeight() { return weight; }
    public void setWeight(int weight) { this.weight = weight; }

    // 添加types的getter方法
    public List<PokemonType> getTypes() {
        return types;
    }
}

3. 在Main类中读取并输出types数组数据

修改Main类的输出部分,遍历解析后的types列表,打印每个类型的信息:

package org.example;
import com.google.gson.Gson;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
        Scanner scan = new Scanner(System.in);
        String name;
        System.out.println("Enter a pokemon name:");
        name = scan.nextLine();
        Gson gson = new Gson();
        HttpClient httpClient = HttpClient.newHttpClient();

        HttpRequest getRequest = HttpRequest.newBuilder()
                .uri(new URI("https://pokeapi.co/api/v2/pokemon/"+name))
                .header("Auth","abc")
                .GET()
                .build();
        HttpResponse<String> getResponse = httpClient.send(getRequest, HttpResponse.BodyHandlers.ofString());
        Transcript transcript = gson.fromJson(getResponse.body(), Transcript.class);
        
        System.out.println("Pokemon name: "+transcript.getName());
        // 遍历types数组并输出
        System.out.println("Pokemon types:");
        for (PokemonType pokemonType : transcript.getTypes()) {
            System.out.println("Slot: " + pokemonType.getSlot() + ", Type Name: " + pokemonType.getType().getName());
        }
    }
}

说明

  • Gson会自动将JSON中的数组映射到Java的List集合,嵌套对象会映射到对应的实体类。
  • 不需要手动处理JSON的解析细节,只要实体类的字段名和JSON的键名一致(大小写敏感),Gson就能完成自动映射。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 06:20:36