Search code examples
javagsonhttpclient

How do i display data inside array in java json


I am trying to fetch an api and i am new in this. I am able to fetch data which are outside in json but i dont know how to display data which are nested inside an array. For example i am trying to fetch data from PokeApi

enter image description here

I am trying to get all data inside 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.HttpRequest.*;
import java.net.http.HttpResponse.BodyHandlers;
import java.net.http.HttpResponse;
import java.util.ArrayList;
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();
        Transcript transcript = new Transcript();
        Gson gson = new Gson();
        String jsonRequest = gson.toJson(transcript);
        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, BodyHandlers.ofString());
        transcript = gson.fromJson(getResponse.body(),Transcript.class);
        System.out.println("Pokemon name: "+transcript.getName());
    }
}

Above is my main file and below is my Transcript class

package org.example;

public class Transcript {
    private String name;


    public int getId() {
        return id;
    }

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

    private int id;
    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    private int height;
    private int order;

    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;
    }

    private int weight;

    public String getName() {
        return name;
    }

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

And I am sorry for my poor english


Solution

  • For nested JSON objects, you need to define appropriate Java classes and make them fields of the parent classes, e.g, in Json you have:

    {
     "forms": [{
       "name": "mew",
       "url": "https://pokeapi.co/api/v2/pokemon-form/151/"
     }]
    }
    

    you need a

    class Form {
     String name;
     java.net.URL url;
    }
    

    and make this a field of your "root" class Transcription

    public class Transcription {
     Form[] forms;
    }
    

    This is a very simplyfied example.