Error in class object generation

Asked

Viewed 60 times

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

2 answers

1

You need to convert the string coming from json to int.

It is not specified, but by the comment in the code, understood that the value is a string:

"idClientesT":"1" // <-- com aspas

For this, you can do so:

int id =  Integer.parseInt(jObect.getString("idClientesT")); 

If I were this way:

"idClientesT":1 // <-- sem aspas

You could use the getInt()


Editing

Before you loop in the Object you need to rescue the array. To then loop and take all the data. It would look like this:

    try {
        JSONArray clientesLista = jObect.getJSONArray("clientes"); // aqui você faz o resgate da lista
        // Transforma a jSon de resposta em um objjeo de Classe
        for (int i = 0; i < clientesLista.length(); i++) {

            int id =  Integer.parseInt(clientesLista.get(i).getString("idClientesT"));
            String tipo = clientesLista.get(i).getString("tipo");
            String nome = clientesLista.get(i).getString("nome");

            Clientes cliente = new Clientes(id, tipo, nome);

            jArray.add(cliente);

      } // <-- estava faltando fechar o loop

    } catch (JSONException e) {
        Log.e("JSON Parser", "Erro no parsing doo objeto " + e.toString());
    }
  • Boy, looking at an error I discovered another. will need to put indexes in the for. Type: String type = jObect.get(i). getString("type");. But it’s not working. How to do?

  • ok. Not to be boring, but already being, please note that the array is a arrray of json called Clients.

  • @Carlosrocha takes a look, this should solve.

  • @Carlosrocha made another issue, had lacked information

  • @Carlosrocha worked? Don’t forget to approve please!

  • So sorry it took so long, it didn’t work. This webservice I’m calling returns a string as follows: {"clients":[{"idClientesT":"1","type":"s","name":"Carlos"},{"idClientesT":"2","type":"s","name":"Rogério"}]}. Then we have an array of objects. So get(i), with the i letter of the iterator in it didn’t work

  • please cancel the issue I accidentally made in my colleague’s post @Andrei Coelho

  • @Carlosrocha but you created Jsonarray?

  • yes, I created Json, it comes from another function. as I put in the question has up to a log. and from it.

Show 4 more comments

0

Well, thanks to the efforts of Mr Andrei, who fortunately deleted his reply, I will say here how the solution has turned out and that it can help other colleagues.

private ArrayList<Clientes> Clientes (JSONObject jObect) {
   // Cria o Array List de Clientes
    ArrayList<Clientes> aCli = null;

    try {

        aCli = new ArrayList<>();
        //Pega o primeiro índice do Array de Objetos, no caso, o array Clientes
        JSONArray clientesLista = jObect.getJSONArray("clientes"); // aqui você faz o resgate da lista

        // Transforma a JSONArray de resposta em um Array de objjeo da Classe Clientes
        for (int i = 0; i < clientesLista.length(); i++) {
            //Pega cada íncide do array e atribui a uma variável
            JSONObject jSobj = clientesLista.getJSONObject(i);
            //Indetifica os campos do objeto
            int id =  Integer.parseInt(jSobj.getString("idClientesT"));
            //int id =  jSobj.getInt("idClientesT");
            String tipo = jSobj.getString("tipo");
            String nome = jSobj.getString("nome");
           //popula o objeto da classe de clientes
            Clientes cliente = new Clientes(id, tipo, nome);
            //Adiciona o objeto de Classe criado ào Array de Clientes
            aCli.add(cliente);
        }

    } catch (JSONException e) {
        Log.e("JSON Parser", "Erro no parsing doo objeto " + e.toString());
    }
    //Retorno o array de Clientes.
    return aCli;

}

Browser other questions tagged

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