Jsonobject does not add Arraylist

Asked

Viewed 57 times

0

I use the code below to popular my arrays quietly, but when researching a topic here, I realized that for my problem I could use a generic class. The problem is that Jsonobject is not populating with the past array.

class GenProjeto {
    int codigo;
    String nome;
    int qtdeHistoricos;
    int qtdeZeros;
    double media;

    @Override
    public String toString() {
        return "Projeto{" + "codigo=" + codigo + ", nome=" + nome + ", qtdeHistoricos=" + qtdeHistoricos + ", qtdeZeros=" + qtdeZeros + ", media=" + media + '}';
    }
}    

List<GenProjeto> projetos = new ArrayList<>();

while (rs.next()) {
    GenProjeto projeto = new GenProjeto();

    projeto.codigo = rs.getInt("his_proj_codigo");
    projeto.nome = rs.getString("proj_nome");
    projeto.qtdeHistoricos = rs.getInt("qtde_historicos");
    projeto.qtdeZeros = rs.getInt("qtde_zeros");
    projeto.media = rs.getDouble("media");

    // exibe o conteudo
    System.out.println(projeto);

    projetos.add(projeto);
}

// exibe o conteudo
System.out.println(projetos);    

JSONObject resposta = new JSONObject();
resposta.put("projetos", projetos);

// exibe vazio
// {"projetos":[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]}
System.out.println(resposta);
return resposta;
  • Billy Jow, I believe the answer here may help in understanding your question.

  • @pss1support, I appreciate the collaboration, but did not understand the relationship of the methods cited in the comment with the add.

  • The use of class Arraylist as the type of the List interface. You have to think about the Designer of the Gendesign class you have to overwrite the equals() and hashcode() methods.

  • And another thing, the access modifier of the Genproject class fields are set to default. This means that all classes should be in the same package. Otherwise the values will not be accessed.

  • Which package is the class JSONObject that you’re using?

  • The Genprojeto class is generic, so it was declared in the same class I’m using. It is only serving as a form or "record" as we do in C++. The Jsonobject class is from lib org.json.

Show 1 more comment
No answers

Browser other questions tagged

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