Hoe te POJO bestand geneste json te creëren voor het krijgen van respons met retrofit

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

    }
}

Boven code die ik heb geprobeerd, maar het krijgen van null.I kreeg de grootte van de serie, maar er is geen data in dat.En als je het niet erg please help me om eruit te komen van dit probleem. heb ik nodig om JSON of POJO bestand. Ik ben op zoek naar gegevens voor kleverige lijst te halen. Mijn gegevens zoals categorie dan subcategorie en daarna de lijst van deze gegevens.

De vraag is gesteld op 09/10/2019 om 12:58
bron van user
In andere talen...                            


2 antwoorden

stemmen
0

1. uw JSON-formaat is verkeerd. hier is de bijgewerkte 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":""
         }
      ]
   ]
}

2. Er moeten twee klassen voor dit Json bestand:

  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+"]";
    }
}

Opmerking : Verwijder onnodige dingen uit code.

Hoop dat dit zal je helpen. gejuich.

antwoordde op 09/10/2019 om 13:19
bron van user

stemmen
0

de fout in ServerResponse klasse, getmenudata is geen arraylist <ArrayList <... >> omdat er al een lijst in Germenulist klasse try:

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

}
antwoordde op 09/10/2019 om 13:10
bron van user

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