Comment créer un fichier POJO de JSON imbriqué pour obtenir une réponse avec la modernisation

voix
-1
How can I create pojo file for nested json to get response with retrofit.
Follwing are my json.
{
    Category_Name: [
        Breads,
        Combo and Thali,
        Main Course,
        Rice and Biryani
    ],
    Alldishitems: [
        [
            {
                Subcategory: Brown bread,
                DishName: Stuffed Paratha,
                DishPrice: 100,
                Dishquantity: 10,
                Dishdescription: 
            },
            {
                Subcategory: white Bread,
                DishName: Plain Paratha,
                DishPrice: 136,
                Dishquantity: 10,
                Dishdescription: 
            }
        ],
        [
            {
                Subcategory: Combo,
                DishName: Rajma Masala Combo,
                DishPrice: 136,
                Dishquantity: 10,
                Dishdescription: 
            },
            {
                Subcategory: Combo,
                DishName: Lassi with Aloo Paratha,
                DishPrice: 100,
                Dishquantity: 10,
                Dishdescription: 
            },
            {
                Subcategory: Thali,
                DishName: Chinise thali,
                DishPrice: 100,
                Dishquantity: 10,
                Dishdescription: 
            }
        ],
]

Follwing code I have tried but getting null


public class ServerResponse {
    @SerializedName(Category_Name)
    String[] Category_Name;
    @SerializedName(Alldishitems)
    ArrayList<ArrayList<Getmenulist>>getmenudata = new  ArrayList<ArrayList<Getmenulist>>();

    public  ServerResponse(String[] category_Name,ArrayList<ArrayList<Getmenulist>> getmenudata)
    {
        this.Category_Name=category_Name;
        this.getmenudata=getmenudata;
    }

    public String[] getCategory_Name() {
        return Category_Name;
    }

    public ArrayList<ArrayList<Getmenulist>>getGetmenudata() {
        return getmenudata;
    }


}

public class Getmenulist {
    @SerializedName(Alldishitems)
    List<Getmenulist.Getmenudata> data;
    public Getmenulist(List<Getmenulist.Getmenudata> data)
    {
        this.data=data;
    }

    public List<Getmenulist.Getmenudata> getData() {
        return data;
    }
    public class Getmenudata {
        @SerializedName(Subcategory)
        String Subcategory;
        @SerializedName(DishName)
        String DishName;
        @SerializedName(DishPrice)
        String DishPrice;
        @SerializedName(Dishquantity)
        String dishQuantities;
        @SerializedName(Dishdescription)
        String Dishdescription;
        @SerializedName(image)
        String image;


        public Getmenudata(String Subcategory, String DishName, String DishPrice, String dishQuantities, String Dishdescription, String image) {
            this.Subcategory = Subcategory;
            this.DishName = DishName;
            this.DishPrice = DishPrice;
            this.dishQuantities = dishQuantities;
            this.Dishdescription = Dishdescription;
            this.image = image;

        }

        public String getDishdescription() {
            return Dishdescription;
        }

        public String getImage() {
            return image;
        }

        public String getDishName() {
            return DishName;
        }

        public String getDishPrice() {
            return DishPrice;
        }

        public String getDishQuantities() {
            return dishQuantities;
        }

        public String getSubcategory() {
            return Subcategory;
        }

    }
}

Au-dessus de code que je l'ai essayé, mais obtenir null.I obtenu la taille du tableau, mais il n'y a pas de données dans that.and si votre esprit Ne me s'il vous plaît aider à sortir de ce problème. dois-je changer JSON ou fichier POJO. Je suis en train de récupérer les données pour la liste collante. Mes données comme catégorie et sous-catégorie, puis après cette liste de ces données.

Créé 09/10/2019 à 12:58
source utilisateur
Dans d'autres langues...                            


2 réponses

voix
0

1. votre format JSON est erroné. voici la JSON mise à jour:

{ 
   "Category_Name":[ 
      "Breads",
      "Combo and Thali",
      "Main Course",
      "Rice and Biryani"
   ],
   "Alldishitems":[ 
      [ 
         { 
            "Subcategory":"Brown bread",
            "DishName":"Stuffed Paratha",
            "DishPrice":"100",
            "Dishquantity":"10",
            "Dishdescription":""
         },
         { 
            "Subcategory":"white Bread",
            "DishName":"Plain Paratha",
            "DishPrice":"136",
            "Dishquantity":"10",
            "Dishdescription":""
         }
      ],
      [ 
         { 
            "Subcategory":"Combo",
            "DishName":"Rajma Masala Combo",
            "DishPrice":"136",
            "Dishquantity":"10",
            "Dishdescription":""
         },
         { 
            "Subcategory":"Combo",
            "DishName":"Lassi with Aloo Paratha",
            "DishPrice":"100",
            "Dishquantity":"10",
            "Dishdescription":""
         },
         { 
            "Subcategory":"Thali",
            "DishName":"Chinise thali",
            "DishPrice":"100",
            "Dishquantity":"10",
            "Dishdescription":""
         }
      ]
   ]
}

2. Il devrait y avoir deux classes pour ce fichier JSON:

  1. YourName.java
public class PojoClass
{
    private String[] Category_Name;

    private Alldishitems[][] Alldishitems;

    public String[] getCategory_Name ()
    {
        return Category_Name;
    }

    public void setCategory_Name (String[] Category_Name)
    {
        this.Category_Name = Category_Name;
    }

    public Alldishitems[][] getAlldishitems ()
    {
        return Alldishitems;
    }

    public void setAlldishitems (Alldishitems[][] Alldishitems)
    {
        this.Alldishitems = Alldishitems;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [Category_Name = "+Category_Name+", Alldishitems = "+Alldishitems+"]";
    }
}
  1. Allitems.java
public class Alldishitems
{
    private String Subcategory;

    private String DishPrice;

    private String Dishquantity;

    private String Dishdescription;

    private String DishName;

    public String getSubcategory ()
    {
        return Subcategory;
    }

    public void setSubcategory (String Subcategory)
    {
        this.Subcategory = Subcategory;
    }

    public String getDishPrice ()
    {
        return DishPrice;
    }

    public void setDishPrice (String DishPrice)
    {
        this.DishPrice = DishPrice;
    }

    public String getDishquantity ()
    {
        return Dishquantity;
    }

    public void setDishquantity (String Dishquantity)
    {
        this.Dishquantity = Dishquantity;
    }

    public String getDishdescription ()
    {
        return Dishdescription;
    }

    public void setDishdescription (String Dishdescription)
    {
        this.Dishdescription = Dishdescription;
    }

    public String getDishName ()
    {
        return DishName;
    }

    public void setDishName (String DishName)
    {
        this.DishName = DishName;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [Subcategory = "+Subcategory+", DishPrice = "+DishPrice+", Dishquantity = "+Dishquantity+", Dishdescription = "+Dishdescription+", DishName = "+DishName+"]";
    }
}

Remarque : Supprimer les choses inutiles de code.

J'espère que cela vous aidera. à votre santé.

Créé 09/10/2019 à 13:19
source utilisateur

voix
0

l'erreur est en classe ServerResponse, getmenudata n'est pas un ArrayList <arrayList <... >> car il y a déjà une liste essayer de classe Germenulist:

public class ServerResponse {
    @SerializedName("Category_Name")
    String[] Category_Name;
    @SerializedName("Alldishitems")
    List<Getmenulist> getmenudata = new ArrayList<Getmenulist>();

    public ServerResponse(String[] category_Name, ArrayList<Getmenulist> getmenudata) {
        this.Category_Name = category_Name;
        this.getmenudata = getmenudata;
    }

    public String[] getCategory_Name() {
        return Category_Name;
    }

    public List<Getmenulist> getGetmenudata() {
        return getmenudata;
    }


}

public class Getmenulist {

    @SerializedName("Alldishitems")
    List<Getmenudata> data;

    public Getmenulist(List<Getmenulist.Getmenudata> data) {
        this.data = data;
    }

    public class Getmenudata {
        @SerializedName("Subcategory")
        String Subcategory;
        @SerializedName("DishName")
        String DishName;
        @SerializedName("DishPrice")
        String DishPrice;
        @SerializedName("Dishquantity")
        String dishQuantities;
        @SerializedName("Dishdescription")
        String Dishdescription;
        @SerializedName("image")
        String image;


        public Getmenudata(String Subcategory, String DishName, String DishPrice, String dishQuantities, String Dishdescription, String image) {
            this.Subcategory = Subcategory;
            this.DishName = DishName;
            this.DishPrice = DishPrice;
            this.dishQuantities = dishQuantities;
            this.Dishdescription = Dishdescription;
            this.image = image;

        }

        public String getDishdescription() {
            return Dishdescription;
        }

        public String getImage() {
            return image;
        }

        public String getDishName() {
            return DishName;
        }

        public String getDishPrice() {
            return DishPrice;
        }

        public String getDishQuantities() {
            return dishQuantities;
        }

        public String getSubcategory() {
            return Subcategory;
        }
    }

}
Créé 09/10/2019 à 13:10
source utilisateur

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more