1

I'm retrieving data from a Web Service, which returns a String with the JSON representation of my object. What happens is that my object has two ArrayList<> of other objects, besides of other simple attributes. It seems to be a problem for the Gson. I've been searching and the answers do not match my specific problem.

My code is, basically:

Verbete

public class Verbete implements Serializable{
    private long id;
    private List<NomesVerbete> nomes;
    private List<SignificadosVerbete> significados;
    private int totalAcessos = 0;
    private Date dataLancamento;
    private int relevancia = 0;
    //getters and setters
}

NomesVerbete

public class NomesVerbete implements Serializable {
    private long id;
    private String nome;
    private String etimologia;
    //getters and setters
}

SignificadosVerbete

public class SignificadosVerbete implements Serializable{
    private long id;
    private String significado;
    private CategoriaVerbete categoria;
    //getters and setters
}

CategoriaVerbete

public class CategoriaVerbete implements Serializable{
    private long id;
    private String nome;
    private String descricao;
    private int serie;
    //getters and setters
}

The conversion from Verbete to JSON gives me this String (which seems to be correct, right?):

{"id":81,"nomes":[{"id":124,"nome":"aleluia","etimologia":"asasiajisjaijs"},{"id":126,"nome":"amém","etimologia":"asasiajisjaijs"}],"significados":[{"id":67,"significado":"asasjaijsiajsoijaoisjaoisj","categoria":{"id":3,"nome":"Nada","descricao":"asuahushaus","serie":1}}],"totalAcessos":0,"dataLancamento":1382066568000,"relevancia":0}

But, when I try to convert this JSON string to Verbete, it doesn't work, comproved by the NullPointerException that I get when trying to access the object. Here's is my conversion from Json:

Gson gson = new GsonBuilder().create();
Verbete verbete = gson.fromJson(resposta[1], Verbete.class); //resposta[1] is the json string above

Could someone give me a way to go in this conversion, please? Thank you.

5
  • 1
    And the stack trace of the exception is? Commented Nov 10, 2013 at 15:21
  • LogCat just show a NullPointerException when I try to access the "verbete" object. Commented Nov 10, 2013 at 15:23
  • I seriously doubt it. Show us an SSCCE reproducing the problem. Commented Nov 10, 2013 at 15:26
  • 1
    I'm guessing you're either feeding the parser a null string or you're not actually checking the result from the parser. Commented Nov 10, 2013 at 15:27
  • The String resposta[1] actually has the Json string. As I can see, my code should work correctly, right? Commented Nov 10, 2013 at 15:34

1 Answer 1

1

do like this

    final String json = "{\"id\":81,\"nomes\":[{\"id\":124,\"nome\":\"aleluia\",\"etimologia\":\"asasiajisjaijs\"},{\"id\":126,\"nome\":\"amém\",\"etimologia\":\"asasiajisjaijs\"}],\"significados\":[{\"id\":67,\"significado\":\"asasjaijsiajsoijaoisjaoisj\",\"categoria\":{\"id\":3,\"nome\":\"Nada\",\"descricao\":\"asuahushaus\",\"serie\":1}}],\"totalAcessos\":0,\"dataLancamento\":1382066568000,\"relevancia\":0}";
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Date.class,
            new JsonDeserializer<Date>() {
                @Override
                public Date deserialize(JsonElement jsonElement, Type type,
                        JsonDeserializationContext context)
                        throws JsonParseException {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(jsonElement.getAsLong());
                    return calendar.getTime();
                }
            });
    Gson gson = gsonBuilder.create();
    Verbete verbete = gson.fromJson(json, Verbete.class);

    System.out.println(verbete);
Sign up to request clarification or add additional context in comments.

1 Comment

Interesting! It worked! So, the problem was in to convert from milliseconds to Date object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.