I have an error trying to consume an API, via js fExpected BEGIN_OBJECT but was BEGIN_ARRAY

Asked

Viewed 40 times

0

I’m trying this error because I’m picking up (from an api) a json, does anyone help me how to solve? I’m a beginner in JSF. This is my code to retrieve the data

@Named
@ViewScoped
public class DatasMBean implements Serializable {


private datas datas;
private String dados;
private String estado;


public datas getDatas() {
    return datas;
}

public void setDatas(datas datas) {
    this.datas = datas;
}

public String getEstado() {
    return estado;
}

public void setEstado(String estado) {
    this.estado = estado;
}

public void buscarDatas(){

    String URL = "https://api.calendario.com.br/?json=true&ano=2019&estado=SC&cidade=JARAGUA_DO_SUL&token=ZmVsaXBlLm1vdGFlZmZ0aW5nQGdtYWlsLmNvbSZoYXNoPTE0NDA1Nzc2MA";

    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(URL);

    Response response = target.request().get();
    //String json = response.readEntity(String.class);
    String json = response.readEntity(String.class);

    response.close();


    datas = new Gson().fromJson(json, datas.class);
    System.out.println(datas);

}
}

Here is the code of my class that would like to save the api data.

public class datas {

private String[] date;
private String[] name;
private String[] type;
private String[] description;

public String[] getDate() {
    return date;
}

public void setDate(String[] date) {
    this.date = date;
}

public String[] getName() {
    return name;
}

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

public String[] getType() {
    return type;
}

public void setType(String[] type) {
    this.type = type;
}

public String[] getDescription() {
    return description;
}

public void setDescription(String[] description) {
    this.description = description;
}

The error I get is this: Expected BEGIN_OBJECT but was BEGIN_ARRAY The json I’m trying to catch is here: https://api.calendario.com.br/? json=true&ano=2017&estado=Sp&cidade=SAO_PAULO&token=Zmvsaxbllm1vdgflzmz0aw5nqgdtywlslmnvbszoyxnopte0nda1nzc2ma

1 answer

0

Your class does not correctly represent the API response.

The correct statement would be:

public class Data {

    private String date;
    private String name;
    private String link;
    private String type;
    private String description;

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getName() {
    return name;
    }

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

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

And to deserialize, use Typetoken:

Type collectionType = new TypeToken<Collection<Data>>(){}.getType();
Collection<Data> datas = gson.fromJson(json, collectionType);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.