2
I have the following method:
private ArrayList Clientes (JSONObject jObect) {
Log.e("Lis: ", jObect.toString());
/*
A linha acima impime
E/Lis:: {"clientes":[{"idClientesT":"1","tipo":"s","nome":"Carlos"},{"idClientesT":"2","tipo":"s","nome":"Rogério"}]}
*/
ArrayList jArray = new ArrayList();
try {
// Transforma a jSon de resposta em um objjeo de Classe
for (int i = 0; i < jObect.length(); i++) {
int id = jObect.getInt("idClientesT");
String tipo = jObect.getString("tipo");
String nome = jObect.getString("nome");
Clientes cliente = new Clientes(id, tipo, nome);
jArray.add(cliente);
} catch (JSONException e) {
Log.e("JSON Parser", "Erro no parsing doo objeto " + e.toString());
}
return jArray;
}
That returns me one ArrayList
of clients class objects that have the identifier attribute, idClientesT
, like int
It turns out that in the generation of the class object:
for (int i = 0; i < jObect.length(); i++) {
int id = jObect.getInt("idClientesT");
Is making a mistake:
Here is the class:
package carcleo.com.radiosingular.classes;
public class Clientes {
private int idClientesT;
private String tipo;
private String nome;
public Clientes(int idClientesT, String tipo, String nome) {
this.idClientesT = idClientesT;
this.tipo = tipo;
this.nome = nome;
}
public int getIdClientesT() {
return idClientesT;
}
public String getTipo() {
return tipo;
}
public String getNome() {
return nome;
}
}
What that part would look like,
int id = jObect.getInt("idClientesT");
so that this error did not occur?
Editing:
This Json I’m calling returns a string as follows:
{
"clientes":
[
{"idClientesT":"1","tipo":"s","nome":"Carlos"},
{"idClientesT":"2","tipo":"s","nome":"Rogério"}
]
}
I tried it the way down and it didn’t work either:
private ArrayList Clientes (JSONObject jObect) {
ArrayList jArray = new ArrayList();
try {
JSONObject jb = jObect.getJSONObject("Clientes");
// Transforma a jSon de resposta em um objjeo de Classe
for (int i = 0; i < jb.length(); i++) {
JSONObject jbi = jb.getJSONObject(i);
int id = Integer.parseInt(jb.getString("idClientesT"));
//int id = jObect.getInt("idClientesT");
String tipo = jb.getString("tipo");
String nome = jb.getString("nome");
Clientes cliente = new Clientes(id, tipo, nome);
jArray.add(cliente);
}
} catch (JSONException e) {
Log.e("JSON Parser", "Erro no parsing doo objeto " + e.toString());
}
return jArray;
}
Calm down, Andrei. I still want to thank you for the effort. If I may, I asked another question because your answer gave me a north, a direction. https://answall.com/questions/352304/falha-na-adi%C3%A7%C3%A3o-de-objects-class-to-array-list
– Carlos Rocha