如何创建嵌套json的pojo文件以通过改造获得响应

时间:2019-10-09 09:58:45

标签: android

如何为嵌套json创建pojo文件,以获取改装响应。以下是我的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": ""
                }
            ],
    ]

我尝试过的下面的代码但是却为空

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

    }
}

上面的代码我尝试过但得到null。我得到了array的大小,但是其中没有数据。如果您不介意请帮助我解决这个问题。 我需要更改json或pojo文件吗?我正在尝试获取粘性列表的数据。我的数据(例如“类别”)然后是子类别,然后在该数据列表之后。

2 个答案:

答案 0 :(得分:0)

错误发生在ServerResponse类中,getmenudata不是arraylist >,因为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;
        }
    }

}

答案 1 :(得分:0)

1。您的json格式错误。这是更新的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。此Json文件应该有两个类:

  
      
  1. YourName.java
  2.   
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
  2.   
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+"]";
    }
}

注意:从代码中删除不必要的内容。

希望这会对您有所帮助。欢呼。