Convert array json into retrofit

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: TipoPrato and Plates.

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

I'm new to retrofit.

Would it take two classes?

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

Class dish

class Prato{
   private String prato;

}

My interface

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

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

My goal then is to make an expandable list

    
asked by anonymous 31.07.2017 / 21:08

2 answers

3
  
    

Convert Json Array into Retrofit (2)

  

1 - Define the template classes that will be used to map the JSON data:

Cardapio.java

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;
    }

}

Prato.java

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 ondes will define all possible HTTP operations:

CardapioService.java

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

public interface CardapioService {

    @GET("/restaurante")
    Call<CardapioRequest> getCardapio();
}
    
30.12.2017 / 09:23
0

You have the array of pratos within the array of cardapio that is in an object. Basically this solves 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 look something like:

@GET("/restaurante")
Call<Cardapio.Conteudo> getCardapio();
    
31.07.2017 / 22:22