自動生成された pojo クラスから json 配列データを取得して表示する方法は?

安全な

インターネット全体を検索しましたが、pojo クラスから json 配列を解析できません。アプリケーションが何度もクラッシュします。複雑な json 応答から 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
}

] }

スミット

ここでは、次のクラスを使用して応答を解析できます。2 つのクラスを作成します: 1. DepartmentAndCategory 2. ProductsAndDeals

//DepartmentAndCategory クラス

パブリック クラス DepartmentAndCategory {

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 クラス

パブリック クラス ProductsAndDeals { プライベート文字列 productName;

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

}

// 次に、応答を解析するメイン クラス

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


}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

jackson javaを使用したJSONからPOJO to Objectクラス

分類Dev

抽象クラスを使用してJSONデータの配列をPOJOに逆シリアル化するにはどうすればよいですか?

分類Dev

オブジェクト構成でpojoからフラット化されたpojoを自動生成する方法はありますか

分類Dev

JSONからPOJO:一意のJSONフィールドから抽象Javaクラス名を生成する方法は?

分類Dev

pojoクラスを介してレトロフィット応答からjson配列にネストされたjsonオブジェクトを取得するにはどうすればよいですか?

分類Dev

動的JSONデータをJavaのpojoクラスにマップしますか?

分類Dev

Jacksonを使用してPOJOからネストされたJSONを形成する

分類Dev

JSON配列から選択したデータを取得する方法は?

分類Dev

Retrofit2.0からのJSON応答によって作成されたPOJOクラスオブジェクトをコピーおよび変更する方法

分類Dev

Androidでネストされた動的Jsonオブジェクトpojoクラスを作成する方法

分類Dev

自動生成された<jsonObject>をjsonからxmlへのコンバーターから削除する方法

分類Dev

POJOで動的キーを使用してこのJSONを設定し、Android JAVAでデータを取得するにはどうすればよいですか?

分類Dev

ネストされたjson配列androidからデータを取得する方法

分類Dev

GSONを使用したJSONからPOJOへ

分類Dev

Gson を介した JSON からの POJO

分類Dev

POJOでロンボクを使用して、自動生成されたセッター(@Dataを使用)でプロパティ値を自動トリミングするにはどうすればよいですか?

分類Dev

IntelliJのデータベースからPOJOクラスを自動作成

分類Dev

phpを使用してjsonのネストされた配列からデータを取得する

分類Dev

ネストされたjson配列からデータを抽出する方法は?

分類Dev

swagger.jsonを使用してFlurlの自動生成されたコードはありますか?

分類Dev

さまざまなJSONを介してマッピングし、配列からデータを取得する方法

分類Dev

JSONスキーマから生成されたPOJOの形式を変更するにはどうすればよいですか?

分類Dev

POJOからJsonスキーマを生成できません

分類Dev

Generic POJO to JSONを使用して、JacksonとGenericクラスの動的jsonキーを使用

分類Dev

AndroidはPojoクラスにjson配列を追加します

分類Dev

jsonのネストされた配列データを取得してリストビューに入力する方法は?

分類Dev

JSONから取得したデータの配列を作成する

分類Dev

Json をネストされた pojo にデシリアライズします。

分類Dev

Excelvba-jsonでJSONネストされた配列からデータを取得する方法

Related 関連記事

  1. 1

    jackson javaを使用したJSONからPOJO to Objectクラス

  2. 2

    抽象クラスを使用してJSONデータの配列をPOJOに逆シリアル化するにはどうすればよいですか?

  3. 3

    オブジェクト構成でpojoからフラット化されたpojoを自動生成する方法はありますか

  4. 4

    JSONからPOJO:一意のJSONフィールドから抽象Javaクラス名を生成する方法は?

  5. 5

    pojoクラスを介してレトロフィット応答からjson配列にネストされたjsonオブジェクトを取得するにはどうすればよいですか?

  6. 6

    動的JSONデータをJavaのpojoクラスにマップしますか?

  7. 7

    Jacksonを使用してPOJOからネストされたJSONを形成する

  8. 8

    JSON配列から選択したデータを取得する方法は?

  9. 9

    Retrofit2.0からのJSON応答によって作成されたPOJOクラスオブジェクトをコピーおよび変更する方法

  10. 10

    Androidでネストされた動的Jsonオブジェクトpojoクラスを作成する方法

  11. 11

    自動生成された<jsonObject>をjsonからxmlへのコンバーターから削除する方法

  12. 12

    POJOで動的キーを使用してこのJSONを設定し、Android JAVAでデータを取得するにはどうすればよいですか?

  13. 13

    ネストされたjson配列androidからデータを取得する方法

  14. 14

    GSONを使用したJSONからPOJOへ

  15. 15

    Gson を介した JSON からの POJO

  16. 16

    POJOでロンボクを使用して、自動生成されたセッター(@Dataを使用)でプロパティ値を自動トリミングするにはどうすればよいですか?

  17. 17

    IntelliJのデータベースからPOJOクラスを自動作成

  18. 18

    phpを使用してjsonのネストされた配列からデータを取得する

  19. 19

    ネストされたjson配列からデータを抽出する方法は?

  20. 20

    swagger.jsonを使用してFlurlの自動生成されたコードはありますか?

  21. 21

    さまざまなJSONを介してマッピングし、配列からデータを取得する方法

  22. 22

    JSONスキーマから生成されたPOJOの形式を変更するにはどうすればよいですか?

  23. 23

    POJOからJsonスキーマを生成できません

  24. 24

    Generic POJO to JSONを使用して、JacksonとGenericクラスの動的jsonキーを使用

  25. 25

    AndroidはPojoクラスにjson配列を追加します

  26. 26

    jsonのネストされた配列データを取得してリストビューに入力する方法は?

  27. 27

    JSONから取得したデータの配列を作成する

  28. 28

    Json をネストされた pojo にデシリアライズします。

  29. 29

    Excelvba-jsonでJSONネストされた配列からデータを取得する方法

ホットタグ

アーカイブ