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.
resposta[1]actually has the Json string. As I can see, my code should work correctly, right?