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.
– pss1suporte
@pss1support, I appreciate the collaboration, but did not understand the relationship of the methods cited in the comment with the add.
– Billly Jow
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.
– pss1suporte
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.
– pss1suporte
Which package is the class
JSONObject
that you’re using?– igventurelli
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.
– Billly Jow