0

I am trying to convert the following Json into a Java class. For some reason, Gson is not binding the list of Ecs. The list of ecs is returning null. I have to do something for Gson to recognize the list. Please, What am I doing wrong?

These are my classes:

public class ConsultaProdutos {

    public String code;
    public String mensagem;
    public List<Retorno> retorno;

    // getters and setters
}

public class Retorno {
    public String idProduto;
    public String descricaoProduto;
    public String cardType;
    public List<ECs> ecs;

   //getters and setters
}

public class ECs {
    public String codigoEC;
    public String codigoTabelaCMM;
    public boolean habilitado;
    public boolean restricaoHabilitacao;

   //getters and setters
}

public class Teste {

public static void main(String[] args) throws JsonSyntaxException, JsonIOException, FileNotFoundException {
    // TODO Auto-generated method stub
    
    Gson gson = new Gson();
    ConsultaProdutos consulta = gson.fromJson(new FileReader("D:\\ConsultaProdutos.json"), ConsultaProdutos.class);

    for (Retorno retorno : consulta.getRetorno()) {
        if (retorno.getIdProduto().equals("18")) {
            List<ECs> ecs = retorno.getEcs();
            for (ECs ec : ecs) {
                if (ec.isHabilitado()) {
                    System.out.println("Tem Loja Virtual");
                } else {
                    System.out.println("Não tem Loja Virtual");
                }
                    
            }
        }
    }

}

And this is the Json:

{
"code": 200,
"mensagem": "Consulta realizada com sucesso.",
"retorno": [
    {
        "idProduto": 18,
        "descricaoProduto": "Loja Digital",
        "cardType": "LJVT",
        "ECs": [
            {
                "codigoEC": 179,
                "codigoTabelaCMM": "A419",
                "habilitado": false,
                "restricaoHabilitacao": false
            }
        ]
    }
]
}
David Brossard
  • 13,584
  • 6
  • 55
  • 88

1 Answers1

0

You have a case issue. The key is ECs and not ecs. Check this answer for details. Valeu!

Sergio Santiago
  • 1,316
  • 1
  • 11
  • 19