如何从我们自动生成的 pojo class 中获取和显示 json 数组数据?

how to get and display json array data from our auto generated pojo class?

我在整个互联网上进行了搜索,但无法从 pojo 解析我的 json 数组 class.Application 再次崩溃 again.Please 谁能告诉我如何解析并从复杂的 json response.This 中显示 json 数组是我的 JSON 响应。 从这里我想解析我的部门名称、描述、产品名称和类别列表。

{ "departmentAndCategory": [
{
  "departmentName": "Apparels",
  "departmentDescription": "Apparels",
  "storeId": null,
  "categoryList": [],
  "id": 1
},
{
  "departmentName": "Footwear",
  "departmentDescription": "footwear for men,women and kids",
  "storeId": null,
  "categoryList": [
    {
      "departmentId": 10,
      "categoryName": "Footwear",
      "categoryDescription": "Footwear for men,women and kids",
      "parentCategoryId": null
    },
    {
      "departmentId": 10,
      "categoryName": "Vehicles",
      "categoryDescription": "Two and Four wheelers",
      "parentCategoryId": null
    }
  ],
  "id": 10
},
{
  "departmentName": "Appliances",
  "departmentDescription": "TV,Washing Machines,Refrigerators etc.",
  "storeId": null,
  "categoryList": [
    {
      "departmentId": 11,
      "categoryName": "Appliances",
      "categoryDescription": "Appliances for home like TV,Washing Machines,Refrigerators etc.",
      "parentCategoryId": null
    },
    {
      "departmentId": 11,
      "categoryName": "Vehicles",
      "categoryDescription": "Two and Four wheelers",
      "parentCategoryId": null
    }
  ],
  "id": 11
},
{
  "departmentName": "Electronics",
  "departmentDescription": "Mobile Phones,Routers etc.",
  "storeId": null,
  "categoryList": [
    {
      "departmentId": 12,
      "categoryName": "Electronics",
      "categoryDescription": "Electrnocs for personal use like Mobile Phones,Routers etc.",
      "parentCategoryId": null
    }
  ],
  "id": 12
},
{
  "departmentName": "Home & Furniture",
  "departmentDescription": "All your Home & Furniture needs",
  "storeId": null,
  "categoryList": [],
  "id": 2
},
{
  "departmentName": "test",
  "departmentDescription": "test",
  "storeId": null,
  "categoryList": [],
  "id": 14
},
{
  "departmentName": "apparels",
  "departmentDescription": "apparels",
  "storeId": null,
  "categoryList": [],
  "id": 15
},
{
  "departmentName": "Levi`s Tshirt",
  "departmentDescription": "Levi`s Tshirt",
  "storeId": null,
  "categoryList": [],
  "id": 16
}],"productsAndDeals": [ {
  "imageName": null,
  "smallImage": null,
  "productName": "Levis T-shirt",
  "productDescription": "A good fabric from levis",
  "productPrice": 2000,
  "productInStock": 7,
  "productAvailability": "Product is available",
  "title": "Clothes",
  "dealList": [],
  "id": 1
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "skin-fit",
  "productDescription": "best blue jeans",
  "productPrice": 1000,
  "productInStock": 15,
  "productAvailability": "Product is available",
  "title": "Jeans",
  "dealList": [
    {
      "dealImage": "dev.beacon.interrait.com/resources/deal-1.jpg",
      "productsId": 2,
      "categoryId": 1,
      "expiryDate": null,
      "discountPercentage": 60,
      "discountAmount": 600,
      "isActive": "False",
      "dealNotes": null,
      "dealName": "Massive discount on skin fit jeans",
      "id": 1
    }
  ],
  "id": 2
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "bell-bottoms",
  "productDescription": "best blue jeans",
  "productPrice": 1200,
  "productInStock": 15,
  "productAvailability": "Product is available",
  "title": "Jeans",
  "dealList": [
    {
      "dealImage": "dev.beacon.interrait.com/resources/deal-1.jpg",
      "productsId": 3,
      "categoryId": 1,
      "expiryDate": null,
      "discountPercentage": 40,
      "discountAmount": 480,
      "isActive": "False",
      "dealNotes": null,
      "dealName": "Massive discount on bell bottoms",
      "id": 2
    }
  ],
  "id": 3
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "curve-jeans",
  "productDescription": "best blue jeans",
  "productPrice": 1200,
  "productInStock": 15,
  "productAvailability": "Product is available",
  "title": "Jeans",
  "dealList": [
    {
      "dealImage": "dev.beacon.interrait.com/resources/deal-1.jpg",
      "productsId": 4,
      "categoryId": 1,
      "expiryDate": null,
      "discountPercentage": 50,
      "discountAmount": 600,
      "isActive": "False",
      "dealNotes": null,
      "dealName": "Massive discount on curve jeans",
      "id": 3
    }
  ],
  "id": 4
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "Aman",
  "productDescription": "",
  "productPrice": 1000,
  "productInStock": 12,
  "productAvailability": "Product is available",
  "title": "Tshirt",
  "dealList": [],
  "id": 6
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "T-shirts for Men",
  "productDescription": "New T-shirt collection",
  "productPrice": 599,
  "productInStock": 0,
  "productAvailability": "Product not in stock",
  "title": "T-shirts",
  "dealList": [],
  "id": 7
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "abvgc",
  "productDescription": "askkkdd",
  "productPrice": null,
  "productInStock": 1,
  "productAvailability": "Product is available",
  "title": "dddrr",
  "dealList": [],
  "id": 19
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "top",
  "productDescription": "askkkdd",
  "productPrice": null,
  "productInStock": 1,
  "productAvailability": "Product is available",
  "title": "dddrr",
  "dealList": [],
  "id": 20
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "topsss",
  "productDescription": "askkkdfffd",
  "productPrice": null,
  "productInStock": 1,
  "productAvailability": "Product is available",
  "title": "dddrr",
  "dealList": [],
  "id": 21
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "topsss",
  "productDescription": "askkkdfffd",
  "productPrice": null,
  "productInStock": 1,
  "productAvailability": "Product is available",
  "title": "dddrr",
  "dealList": [],
  "id": 22
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "sample",
  "productDescription": "sample",
  "productPrice": null,
  "productInStock": 10,
  "productAvailability": "Product is available",
  "title": "Product",
  "dealList": [],
  "id": 23
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "sample",
  "productDescription": "sample",
  "productPrice": null,
  "productInStock": 10,
  "productAvailability": "Product is available",
  "title": "Product",
  "dealList": [],
  "id": 24
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "sample",
  "productDescription": "sample",
  "productPrice": null,
  "productInStock": 10,
  "productAvailability": "Product is available",
  "title": "Product",
  "dealList": [],
  "id": 25
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "mobile",
  "productDescription": "smartphones",
  "productPrice": null,
  "productInStock": 1,
  "productAvailability": "Product is available",
  "title": "dddrr",
  "dealList": [],
  "id": 30
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "Apples",
  "productDescription": "fresh apples from farm",
  "productPrice": 150,
  "productInStock": 90,
  "productAvailability": "Product is available",
  "title": "Red apples",
  "dealList": [],
  "id": 31
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "slippers and jackets",
  "productDescription": "Slip Ons",
  "productPrice": 210,
  "productInStock": 45,
  "productAvailability": "Product is available",
  "title": "Liberty slippes",
  "dealList": [],
  "id": 110
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "glucose",
  "productDescription": "glucone D",
  "productPrice": 60,
  "productInStock": 123,
  "productAvailability": "Product is available",
  "title": "Glucose for health",
  "dealList": [],
  "id": 117
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "jackets",
  "productDescription": "rugged jackets",
  "productPrice": 210,
  "productInStock": 45,
  "productAvailability": "Product is available",
  "title": "Liberty slippes",
  "dealList": [],
  "id": 121
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "Stationary",
  "productDescription": "pens,pencil",
  "productPrice": 10,
  "productInStock": 100,
  "productAvailability": "Product is available",
  "title": "--",
  "dealList": [],
  "id": 127
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "bat",
  "productDescription": "cricket bats",
  "productPrice": 500,
  "productInStock": 20,
  "productAvailability": "Product is available",
  "title": "games for kids",
  "dealList": [],
  "id": 128
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "ball",
  "productDescription": "cricket bats",
  "productPrice": 500,
  "productInStock": 20,
  "productAvailability": "Product is available",
  "title": "games for kids",
  "dealList": [],
  "id": 130
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "sports",
  "productDescription": "cricket bats",
  "productPrice": 500,
  "productInStock": 20,
  "productAvailability": "Product is available",
  "title": "games for kids",
  "dealList": [],
  "id": 131
},
{
  "imageName": null,
  "smallImage": null,
  "productName": "garden",
  "productDescription": "axe",
  "productPrice": 500,
  "productInStock": 20,
  "productAvailability": "Product is available",
  "title": "-",
  "dealList": [],
  "id": 132
}

] }

在这里您可以使用以下 classes 来解析您的响应: 创建两个 classes: 1.部门和类别 2. 产品和交易

//部门和类别class

public class 部门和类别 {

private List<CategoryList> categoryList;

private String departmentDescription;

private String departmentName;

public DepartmentAndCategory(String departmentName,String departmentDescription,
                             List<CategoryList> categoryList){
    this.departmentName = departmentName;
    this.departmentDescription = departmentDescription;
    this.categoryList = categoryList;
}

public List<CategoryList> getCategoryList() {
    return categoryList;
}

public void setCategoryList(List<CategoryList> categoryList) {
    this.categoryList = categoryList;
}

public String getDepartmentDescription ()
{
    return departmentDescription;
}

public void setDepartmentDescription (String departmentDescription)
{
    this.departmentDescription = departmentDescription;
}

public String getDepartmentName ()
{
    return departmentName;
}

public void setDepartmentName (String departmentName)
{
    this.departmentName = departmentName;
}

@Override
public String toString()
{
    return "ClassPojo [ categoryList = "+categoryList+", departmentDescription = "+departmentDescription+", departmentName = "+departmentName+"]";
}

}

//ProductsAndDeals class

public class ProductsAndDeals { 私有字符串产品名称;

public ProductsAndDeals(){}

public ProductsAndDeals(String productName){
    this.productName = productName;
}

public String getProductName() {
    return productName;
}

public void setProductName(String productName) {
    this.productName = productName;
}

@Override
public String toString()
{

    return "ClassPojo [productName = "+productName+"]";
}

}

// 然后主要class解析响应

public class MainActivity extends AppCompatActivity {


    List<DepartmentAndCategory> departmentAndCategoryList = new ArrayList<>();
    List<ProductsAndDeals> productsAndDealsList = new ArrayList<>();

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int i=0;
        //Pass your response String here
        ParseResponse(Response.RESPONSE);
        String[] departNameArray = new String[departmentAndCategoryList.size()];

        //printing Department Name, Department Description and Category List
        for (DepartmentAndCategory list : departmentAndCategoryList){
            Log.e(TAG,"Departname : "+list.getDepartmentName());
            Log.e(TAG,"DepartDesc :"+list.getDepartmentDescription());
            departNameArray[i] = list.getDepartmentName();
            i++;
            for (CategoryList list1 : list.getCategoryList()){
                Log.e(TAG,"departmentId "+list1.getDepartmentId());
                Log.e(TAG,"categoryName "+list1.getCategoryName());
                Log.e(TAG,"categoryDescription "+list1.getCategoryDescription());
                Log.e(TAG,"parentCategoryId "+list1.getParentCategoryId());
            }
        }

        //departName array
        for (int j=0;j<departNameArray.length;j++){
            Log.e(TAG,"DepartName : "+departNameArray[j]);
        }

        //printing Product Name
        for (ProductsAndDeals dealsList : productsAndDealsList){
            Log.e(TAG,"Product name : "+dealsList.getProductName());
        }

    }

    private void ParseResponse(String response){

        try {
            JSONObject rootObj = new JSONObject(response);
            JSONArray department = rootObj.optJSONArray("departmentAndCategory");

            for (int i=0;i<department.length();i++){
                List<CategoryList> mCategoryList = new ArrayList<>();

                JSONObject departmentObj = department.getJSONObject(i);
                String departName = departmentObj.getString("departmentName");
                String departDesc = departmentObj.getString("departmentDescription");

                JSONArray categoryArray = departmentObj.optJSONArray("categoryList");

                    for (int j=0;j<categoryArray.length();j++){
                        JSONObject categoryObj = categoryArray.getJSONObject(j);
                        String departId = categoryObj.getString("departmentId");
                        String categoryName = categoryObj.getString("categoryName");
                        String categoryDescription = categoryObj.getString("categoryDescription");
                        String parentCategoryId = categoryObj.getString("parentCategoryId");
                        CategoryList categoryList = new CategoryList(categoryName,categoryDescription,
                                departId,parentCategoryId);
                        mCategoryList.add(categoryList);
                    }

                DepartmentAndCategory departmentAndCategory = new DepartmentAndCategory(
                        departName,departDesc,mCategoryList
                );

                departmentAndCategoryList.add(departmentAndCategory);
            }

            JSONArray product = rootObj.optJSONArray("productsAndDeals");
            for (int i=0;i<product.length();i++){
                JSONObject productObj = product.getJSONObject(i);
                String productName = productObj.getString("productName");
                ProductsAndDeals productsAndDeals = new ProductsAndDeals(productName);

                productsAndDealsList.add(productsAndDeals);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


}