JSON parse class java

Asked

Viewed 156 times

2

I’m having some doubts, as I do to parse a JSON that is coming from the webservice.

My class comentarios is like this:

public class Comentarios extends BaseEntity {

 private Integer avaliacao;
 private String observacaoAvaliacao;
 private String nmCliente;

 public Comentarios(Integer avaliacao, String observacaoAvaliacao, String nmCliente) {
    avaliacao = avaliacao;
    observacaoAvaliacao = observacaoAvaliacao;
    nmCliente = nmCliente;
 }

 public void setAvaliacao(Integer avaliacao) {
    this.avaliacao = avaliacao;
 }

 public void setObservacaoAvaliacao(String observacaoAvaliacao) {
    this.observacaoAvaliacao = observacaoAvaliacao;
 }

 public void setNmCliente(String nmCliente) {
    this.nmCliente = nmCliente;
 }

 public Integer getAvaliacao() {
    return avaliacao;
 }

 public String getObservacaoAvaliacao() {
    return observacaoAvaliacao;
 }

 public String getNmCliente() {
    return nmCliente;
 }
}

My JSON is coming from the server like this:

Resposta: {
  "sucesso": true,
  "mensagem": " Comentarios listados com sucesso. ",
  “comentarios”:[
     {
       “avaliacao”:”4”,
       “observacaoAvaliacao”:”Restaurante muito bom, valeu a pena comer o crepe de frango, e muito melhor é o aplicativo.”,
      “nmCliente”:”Renan R.”
     }
  ]
 }

I need access like this:

Comentarios comentarios;
comentarios.getNmCliente();

1 answer

1


If I understand correctly your question is just to assign the values to the comment object and then remove the name with this method, it can be like this...

Comentario comentario = new Comentario();
JSONArray json = new JSONArray(resposta);
JSONObject jsonObj = json.getJSONObject(0); // caso vc só tenha um objeto... caso não vc faz um for... populando uma ArrayList<Comentario>
comentario.setAvaliacao(jsonObj.getInt("avaliacao"));
comentario.setObservacaoAvaliacao(jsonObj.getString("observacaoAvaliacao"));
comentario.setNmCliente(jsonObj.getString("nmCliente"));

now you can remove item by item using the get’s of your class Comment on the comment object that was created... remembering that if you have more than one object in the return of the webserver’s JSON you should use an Arraylist and popular it with the for..

   // Exemplo com for:

    ArrayList<Comentario> listaComentarios = new ArrayList<Comentario>; // cria a lista de comentarios

    JSONArray json = new JSONArray(resposta);

    for(int i = 0; i < json.lenght; i++){
        Comentario comentario = new Comentario();
        JSONObject jsonObj = json.getJSONObject(i); // pega o objeto de indice i
        comentario.setAvaliacao(jsonObj.getInt("avaliacao"));
        comentario.setObservacaoAvaliacao(jsonObj.getString("observacaoAvaliacao"));
        comentario.setNmCliente(jsonObj.getString("nmCliente"));
        listaComentarios.add(comentario); // adiciona o comentario de indice i a lista de comentarios 
    }
  • There are many comments, you can make an example for an array of objects ?

  • To convert JSON to a Java object (POJO) I suggest Google’s Gson library.

  • @Piovezan would like to add an answer with his tip, I think it validates, even if I have already accepted the previous, due to new technologies

  • @Renanrodrigues Just add, you can change the accepted answer after you have already accepted.

  • It is worthwhile then you indicate something better, has as ?

  • @Renanrodrigues did not understand your question. I have indicated something better, which is the Google Gson library.

Show 1 more comment

Browser other questions tagged

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