Convert json array to retrofit

Asked

Viewed 626 times

0

I have the following return in json

{
    "cardapio": [
        {
            "tipoprato": "SALADAS",
            "pratos": [
                {
                    "prato": "ALFACE + REPOLHO ROXO"
                }
            ]
        },
        {
            "tipoprato": "PRATO PRINCIPAL",
            "pratos": [
                {
                    "prato": "ESTROGONOFE DE GR?O DE BICO"
                },
                {
                    "prato": "GALETTE DE PVT, GLUTEN E QUEIJO MUCARELA"
                }
            ]
        },
        {
            "tipoprato": "ACOMPANHAMENTO",
            "pratos": [
                {
                    "prato": "FEIJ?O C/ MAXIXE, QUIABO E COUVE"
                },
                {
                    "prato": "ARROZ BRANCO"
                },
                {
                    "prato": "ARROZ INTEGRAL"
                }
            ]
        },
        {
            "tipoprato": "CALDOS",
            "pratos": [
                {
                    "prato": "CALDO DE COUVE - FLOR"
                }
            ]
        }
    ]
}

At first it would be two classes: Typoprate and Dishes.

My doubt is how I would bring this result to retrofit?

I’m new to retrofit.

It would take two classes?

class Cardapio{
  private String tipo;
  private Prato[] prato; 
  ...
}

Dish class

class Prato{
   private String prato;

}

My interface

GET("/restaurante/")
public void getCardapio(
   Callback<List<Cardapio>> cardapioList;
);

Or it would be better to separate each thing, bring only the Types of Dishes then only the dishes?

My goal later is to make an expandable list

2 answers

4


Convert Json Array to Retrofit(2)

1 - Defining the classes model which will be used to map JSON data:

Cardapio.

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Cardapio {

    @SerializedName("tipoprato")
    @Expose
    private String tipoprato;

    @SerializedName("pratos")
    @Expose
    private List<Prato> pratos = null;

    public String getTipoprato() {
        return tipoprato;
    }

    public void setTipoprato(String tipoprato) {
        this.tipoprato = tipoprato;
    }

    public List<Prato> getPratos() {
        return pratos;
    }

    public void setPratos(List<Prato> pratos) {
        this.pratos = pratos;
    }

}

Java dish.

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Prato {

    @SerializedName("prato")
    @Expose
    private String prato;

    public String getPrato() {
        return prato;
    }

    public void setPrato(String prato) {
        this.prato = prato;
    }

}

Cardapiolist.java

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class CardapioList {

    @SerializedName("cardapio")
    @Expose
    private List<Cardapio> cardapio = null;

    public List<Cardapio> getCardapio() {
        return cardapio;
    }

    public void setCardapio(List<Cardapio> cardapio) {
        this.cardapio = cardapio;
    }

}

2 - Create the Interface where all possible HTTP operations will be defined:

Cardapioservice.java

import retrofit2.Call;
import retrofit2.http.GET;

public interface CardapioService {

    @GET("/restaurante")
    Call<CardapioRequest> getCardapio();
}

0

You have the array of pratos within the array of cardapio that is in an object. Basically this resolves your JSON:

/**
 * Cardapio seria o model para receber o conteúdo do cardapio
 */
public class Cardapio {

    @SerializedName("cardapio")
    private ArrayList<Conteudo> conteudo;

    /**
     * Este é um model que contem o conteúdo que do seu
     * cardapio que tipoprato e um array de pratos.
     */
    public class Conteudo {

        @SerializedName("tipoprato")
        private String tipoprato;

        @SerializedName("pratos")
        private ArrayList<Prato> pratos;
    }

    /**
     * Prato é um model que receberá o nome do prato
     */
    public class Prato {

        @SerializedName("prato")
        public String prato;
    }
}

Your interface would be something like:

@GET("/restaurante")
Call<Cardapio.Conteudo> getCardapio();
  • the way of calling the interface is right there my question?

  • @adventistaam this way ai do not know if it works. I edited my question asking how it would be.

  • Wouldn’t need the getters and setters, no, it’s not?

  • Need, for when you are going to assign the values at some other time. I thought you would already be aware of this. :-)

  • I get it. So it follows the pattern right?

  • Just to redeem the value using retrofit does not need the get and set, but you will need them at some point from your code. I did not put it because you asked only assignments using retrofit. It follows yes the pattern.

  • Blz. Thanks! I’m starting at Retrofit.

  • @adventistaam you came to use this way as I passed?! Give me a feedback on that answer. I saw that you asked another question, but first I wanted you to validate so that I can answer another question, which would be based on this or not. = D

  • I’m actually trying, as I’m studying, I still don’t know many things like: I’m using your suggestion but I’m not being able to visualize in Sponse because it would be a list and sublist. public void onResponse(Call<List<Cardapio>> call, Response<List<Cardapio>> response) {&#xA; List<Cardapio.Conteudo> cardapioResponse = response.body();&#xA; //Percorre a lista e exbie o nome de cada respositório&#xA; for( Conteudo cardapio : cardapioResponse ){&#xA; Log.i("Cardapio", cardapio.getConteudo().toString() );&#xA; }&#xA; }

  • Check out this article by Vogella. I think it will help a lot: http://www.vogella.com/tutorials/Retrofit/article.html

  • Blz. I’m giving him a study too. Thanks

Show 6 more comments

Browser other questions tagged

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