Search code examples
javajsonapiservletsjstl

Getting JSON Data on Table on JSTL Page


So I am using the amiibo api to pull data, and display what is returned in a table. For now, I'm just putting the series in the table to make sure it works, but it doesn't!

Here is my table:

<table class="table table-striped table-hover table-sm">
            <thead>
                <tr>
                    <th class="col-sm-1" scope="col">#</th>
                    <th class="col-sm-11" scope="col">Amiibo</th>
                </tr>
            </thead>
            <c:forEach var="amiibo" items="${amiibos}" varStatus="status">
                <tr>
                    <th scope="row">${status.count}</th>
                    <td>${amiibo.amiiboSeries}</td>
                </tr>
            </c:forEach> 
        </table>

And here is my servlet that pulls that the api data and puts it into a list:

String json = callAmiiboApi(input.getAmiibo());

        // Check for valid json format.  If false, String contains an error message.
        if (json.startsWith("{") || json.startsWith("[")) {
            Gson gson = new Gson();
            ArrayList<Amiibo> amiibos = gson.fromJson(json, new TypeToken<ArrayList<Amiibo>>(){}.getType());

            //get list of elements 
            if (!amiibos.isEmpty() && amiibos.size() > 0) {
                request.setAttribute("amiibos", amiibos);
            }  
        }

Here is my object:

public class Amiibo implements Serializable
{
    public String amiiboSeries;
    public String character;
    public String gameSeries;
    public String head;
    public String image;
    public String name;
    public String[] release;
    public String tail;
    public String type;

    public Amiibo()
    {
        amiiboSeries = "";
        character = "";
        gameSeries = "";
        head = "";
        image = "";
        name = "";
        release = new String[0];
        tail = "";
        type = "";
    }

    public String getAmiiboSeries()
    {
        return amiiboSeries;
    }

This is the error I get:

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Does anyone know what I am doing wrong in pulling this data? I think the api is accessed fine, I believe my issue is in how I am displaying it (potentially when I put it into a list?).

-- edit --

This is my input class to get the user input

import java.io.Serializable;

public class Input implements Serializable {
    private String amiibo;

    public Input() {
        this("");
    }

    public Input(String amiibo) {
        this.amiibo = amiibo;
    }

    public String getAmiibo() {
        return amiibo;
    }

    public void setAmiibo(String amiibo) {
        this.amiibo = amiibo;
    }  
}

Solution

  • Crete wrapper object:

    class AmiiboResponse {
    
        private List<Amiibo> amiibo;
    
        public List<Amiibo> getAmiibo() {
            return amiibo;
        }
    
        public void setAmiibo(List<Amiibo> amiibo) {
            this.amiibo = amiibo;
        }
    }
    

    and deserialise in this way:

    List<Amiibo> amiibos = gson.fromJson(json, AmiiboResponse.class).getAmiibo();