How to get Json Object from json Array?

Haider Shah Hashmi :

I want to get promotion_price from this json file how to get this. I have pasted my code where I tried to get promotion_price but I get null value everytime please show me code how to get these values.

{
"current_page": 1,
"data": [
    {
        "id": 19,
        "sku": "7788",
        "image": "https://hdmart.pk/data/product/Head And Shoulders Scalp Care 360 Ml.jpg",
        "brand_id": 1,
        "vendor_id": 1,
        "price": 420,
        "cost": 420,
        "stock": 0,
        "sold": 1,
        "type": 1,
        "kind": 0,
        "virtual": 0,
        "status": 1,
        "sort": 1,
        "view": 15,
        "alias": "kashmir-oil",
        "date_lastview": "2020-02-27 15:38:47",
        "date_available": null,
        "created_at": "2020-02-21 21:32:04",
        "updated_at": "2020-02-27 15:38:47",
        "name": "Head And Shoulders Scalp Care 360 Ml",
        "keyword": "Head And Shoulders Scalp Care 360 Ml",
        "description": "Head And Shoulders Scalp Care 360 Ml",
        "content": "kashmir oil is best",
        "descriptions": [
            {
                "product_id": 19,
                "lang": "en",
                "name": "Head And Shoulders Scalp Care 360 Ml",
                "keyword": "Head And Shoulders Scalp Care 360 Ml",
                "description": "Head And Shoulders Scalp Care 360 Ml",
                "content": "kashmir oil is best"
            }
        ],
        "promotion_price": null
    },
    {
        "id": 20,
        "sku": "123",
        "image": "https://hdmart.pk/data/product/Ponds Triple Vitamin 200ml.png",
        "brand_id": 7,
        "vendor_id": 1,
        "price": 260,
        "cost": 260,
        "stock": 0,
        "sold": 10,
        "type": 1,
        "kind": 0,
        "virtual": 0,
        "status": 1,
        "sort": 1,
        "view": 12,
        "alias": "ponds-lotion",
        "date_lastview": "2020-02-27 20:58:35",
        "date_available": null,
        "created_at": "2020-02-22 00:01:09",
        "updated_at": "2020-02-27 20:58:35",
        "name": "Ponds Triple Vitamin 200ml",
        "keyword": "Ponds Triple Vitamin 200ml",
        "description": "Ponds Triple Vitamin Moisturising Lotion Silky Smooth Skin 200ML",
        "content": "Ponds Triple Vitamin 200ml ",
        "descriptions": [
            {
                "product_id": 20,
                "lang": "en",
                "name": "Ponds Triple Vitamin 200ml",
                "keyword": "Ponds Triple Vitamin 200ml",
                "description": "Ponds Triple Vitamin Moisturising Lotion Silky Smooth Skin 200ML",
                "content": "Ponds Triple Vitamin 200ml "
            }
        ],
        "promotion_price": {
            "product_id": 20,
            "price_promotion": 255,
            "date_start": null,
            "date_end": null,
            "status_promotion": 1,
            "created_at": "2020-02-27 15:27:24",
            "updated_at": "2020-02-27 15:27:24"
        }
    },
    {
        "id": 21,
        "sku": "0001",
        "image": "https://hdmart.pk/data/product/Tissue Papers.png",
        "brand_id": 3,
        "vendor_id": 1,
        "price": 120,
        "cost": 120,
        "stock": 0,
        "sold": 0,
        "type": 1,
        "kind": 0,
        "virtual": 0,
        "status": 1,
        "sort": 1,
        "view": 11,
        "alias": "fyfy",
        "date_lastview": "2020-02-27 14:40:38",
        "date_available": null,
        "created_at": "2020-02-22 14:37:44",
        "updated_at": "2020-02-27 14:42:40",
        "name": "Rose Petal Pop-Up Ultra Soft",
        "keyword": "Tissue Paper",
        "description": "1 Pack 220 Sheets",
        "content": "gvjhvh",
        "descriptions": [
            {
                "product_id": 21,
                "lang": "en",
                "name": "Rose Petal Pop-Up Ultra Soft",
                "keyword": "Tissue Paper",
                "description": "1 Pack 220 Sheets",
                "content": "gvjhvh"
            }
        ],
        "promotion_price": {
            "product_id": 21,
            "price_promotion": 120,
            "date_start": "2020-02-27 00:00:00",
            "date_end": "2020-02-29 00:00:00",
            "status_promotion": 1,
            "created_at": "2020-02-27 23:27:02",
            "updated_at": "2020-02-27 23:27:02"
        }
    },
    {
        "id": 22,
        "sku": "0004",
        "image": "https://hdmart.pk/data/product/Head And Shoulders Anti-Hairfall 360 Ml.jpg",
        "brand_id": 0,
        "vendor_id": 0,
        "price": 420,
        "cost": 420,
        "stock": 3,
        "sold": 0,
        "type": 1,
        "kind": 0,
        "virtual": 0,
        "status": 1,
        "sort": 0,
        "view": 2,
        "alias": "head-1",
        "date_lastview": "2020-02-27 20:58:30",
        "date_available": null,
        "created_at": "2020-02-27 15:48:17",
        "updated_at": "2020-02-27 20:58:30",
        "name": "Head And Shoulders Anti-Hairfall 360 Ml",
        "keyword": "Head And Shoulders Anti-Hairfall 360 Ml",
        "description": "Head And Shoulders Anti-Hairfall 360 Ml",
        "content": "Head And Shoulders Anti-Hairfall 360 Ml",
        "descriptions": [
            {
                "product_id": 22,
                "lang": "en",
                "name": "Head And Shoulders Anti-Hairfall 360 Ml",
                "keyword": "Head And Shoulders Anti-Hairfall 360 Ml",
                "description": "Head And Shoulders Anti-Hairfall 360 Ml",
                "content": "Head And Shoulders Anti-Hairfall 360 Ml"
            }
        ],
        "promotion_price": null
    },
    {
        "id": 23,
        "sku": "0005",
        "image": "https://hdmart.pk/data/product/Headand Shoulders Classic Clean 360 Ml.jpg",
        "brand_id": 7,
        "vendor_id": 1,
        "price": 420,
        "cost": 420,
        "stock": 3,
        "sold": 0,
        "type": 0,
        "kind": 0,
        "virtual": 0,
        "status": 1,
        "sort": 0,
        "view": 2,
        "alias": "sf",
        "date_lastview": "2020-02-27 20:59:25",
        "date_available": null,
        "created_at": "2020-02-27 15:53:21",
        "updated_at": "2020-02-27 20:59:25",
        "name": "Headand Shoulders Classic Clean 360 Ml",
        "keyword": "Headand Shoulders Classic Clean 360 Ml",
        "description": "Headand Shoulders Classic Clean 360 Ml",
        "content": "Headand Shoulders Classic Clean 360 Ml",
        "descriptions": [
            {
                "product_id": 23,
                "lang": "en",
                "name": "Headand Shoulders Classic Clean 360 Ml",
                "keyword": "Headand Shoulders Classic Clean 360 Ml",
                "description": "Headand Shoulders Classic Clean 360 Ml",
                "content": "Headand Shoulders Classic Clean 360 Ml"
            }
        ],
        "promotion_price": null
    }

Android code:

private void jsonRequestForOffers() {
        jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JSON_URL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
             //   Toast.makeText(getContext(), response.toString(), Toast.LENGTH_SHORT).show();

                try {
                    JSONArray data = response
                            .getJSONArray("data");
                    for (int i=0;i<data.length();i++){
                        JSONObject jOB = data.getJSONObject(i);
                        Anime anime = new Anime();
                        String product_name = jOB.getString("name");
                        anime.setName(product_name);
                        String product_price = jOB.getString("price");
                        anime.setPrice("RS "+product_price);
                        String product_Cut_price = jOB.getString("price");
                        anime.setPriceCut("RS "+product_Cut_price);
//                        JSONObject jsonObject = data.getJSONObject(1);
//                        anime.setPriceCut("RS "+jsonObject.getString("price_promotion"));
                        String product_img = jOB.getString("image");
                        anime.setImgUrlOfferProduct(product_img);
                        lstAnime.add(anime);
//                        String promotion = jOB.getString("price_promotion");
//                        Toast.makeText(getContext(), promotion, Toast.LENGTH_SHORT).show();
                     //   Toast.makeText(getContext(), product_price, Toast.LENGTH_SHORT).show();
                    }


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

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(), "Error" + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue = Volley.newRequestQueue(getContext());
        requestQueue.add(jsonObjectRequest);
    }
Ranjeet Luhar :

First Created POJO Class for Promotional Prices with getters and setters after that get JSONArray from JSONObject and access promotional prices.

JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = jsonObject.getJSONArray("data");  // Declare Model Class of array element

for(int i=0;i<jsonArray.size();i++)
{ 
   PromotionalPrice promotionalPrice = jsonArray.get(i).getPromotionalPrices();
 Log.d(TAG,promotionalPrice.price_promotion.toString());
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174865&siteId=1