ricardoramos
ricardoramos

Reputation: 956

com.google.gson.JsonSyntaxException: Expected a string but was BEGIN_OBJECT at line 1 column 18101 path $.data[0].groups[0]

I have found a lot of answers for this exception, but coudln't find one that helps my case.

I'm trying to use GSON to parse JSON. Here's my code:

public <T> T getObject(String[] caminho, String[] parametros, Class<T> tipoRetorno) {
        T resultado = null;
        WebResource webResource = getWebResource(caminho, parametros);
        ClientResponse response = getBuilder(webResource).get(ClientResponse.class);
        if (response.getStatus() == Status.OK.getStatusCode()) {
            JSONObject json = null;
            try {
                json = new JSONObject(response.getEntity(String.class));
            } catch (ClientHandlerException | UniformInterfaceException | JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            resultado = new Gson().fromJson(json.toString(), tipoRetorno);
        } else if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) {
            String msg = "Getting cross-connections. URI: " + webResource.getURI() + " status: " + response.getStatus()
                    + " " + response.getStatusInfo();
            logger.info(msg);
        } else {
            String msg = "Error getting cross-connections. URI: " + webResource.getURI() + " returned error : "
                    + response.getStatus() + " status: " + response.getStatusInfo();
            logger.info(msg);
        }
        return resultado;
    }

Here's the JSON returned from the 'json':

{"data":[{"thumbs":[{"id":79204454,"updated_at":"2016-12-24T19:54:48.000Z","created_at":"2016-12-24T19:54:48.000Z","filename":"54857bd6-0ccc-48d2-b5c8-8c4483954789_1482608998509","test_case_id":8172839,"url":"https://s3-eu-west-1.amazonaws.com/euthumbtestingbot/54857bd6-0ccc-48d2-b5c8-8c4483954789_1482608998509.jpg","custom":false},{"id":79204455,"updated_at":"2016-12-24T19:54:48.000Z","created_at":"2016-12-24T19:54:48.000Z","filename":"54857bd6-0ccc-48d2-b5c8-8c4483954789_1482609000499","test_case_id":8172839,"url":"https://s3-eu-west-1.amazonaws.com/euthumbtestingbot/54857bd6-0ccc-48d2-b5c8-8c4483954789_1482609000499.jpg","custom":false},{"id":79204456,"updated_at":"2016-12-24T19:54:48.000Z","created_at":"2016-12-24T19:54:48.000Z","filename":"54857bd6-0ccc-48d2-b5c8-8c4483954789_1482609001762","test_case_id":8172839,"url":"https://s3-eu-west-1.amazonaws.com/euthumbtestingbot/54857bd6-0ccc-48d2-b5c8-8c4483954789_1482609001762.jpg","custom":false}...}

And 'tipoRetorno' is class br.usp.icmc.testingbot.beans.AgrupamentoTestes:

package br.usp.icmc.testingbot.beans;

import java.util.List;

public class AgrupamentoTestes {

    private List<Teste> data;
    private Meta meta;

    public List<Teste> getData() {
        return data;
    }

    public void setData(List<Teste> data) {
        this.data = data;
    }

    public Meta getMeta() {
        return meta;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

}

I'm getting this exception:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 18101 path $.data[0].groups[0]

Why can't Gson properly convert my JSON text to my POJO type?

Upvotes: 1

Views: 1660

Answers (1)

Dan
Dan

Reputation: 13170

If you paste the string into a text editor, you'll be able to see row and column numbers at the bottom, and find column 18101 where the error is.

Upvotes: 1

Related Questions