0

I want to access array child and sub child elements from json file that is stored on server,and show them in recyclerview i tried lot of ways but none othem working for me because i don't have knowledge about this library

    {
  "category1": {
    "subcategory1": {
      "products": [
        {
          "title": "Air Fresheners",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        },
        {
          "title": "Cloth Detergent",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        },
        {
          "title": "Milk",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        }
      ]
    },
    "subcategory2": {
      "products": [
        {
          "title": "Air Fresheners",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        },
        {
          "title": "Cloth Detergent",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        },
        {
          "title": "Milk",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        }
      ]
    }
  },
  "category2": {
    "subcategory1": {
      "products": [
        {
          "title": "Air Fresheners",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        },
        {
          "title": "Cloth Detergent",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        },
        {
          "title": "Milk",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        }
      ]
    },
    "subcategory2": {
      "products": [
        {
          "title": "Air Fresheners",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        },
        {
          "title": "Cloth Detergent",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        },
        {
          "title": "Milk",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        }
      ]
    }
  }
}

i tried many way without having knowledge of them but none of that working for me. It gives error as shown below

of type org.json.JSONObject cannot be converted to JSONArray

my code is

private void getData() {
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Loading...");
    progressDialog.show();

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {


            for (int i = 0; i < response.length(); i++) {
                try {

                    JSONObject jsonObjectRoot = response.getJSONObject("category1");
                    JSONObject jsonObjectSub = jsonObjectRoot.getJSONObject("subcategory1");
                    JSONArray jsonArrayForSub = jsonObjectSub.getJSONArray("products");
                    JSONObject eachObjectInArray = jsonArrayForSub.getJSONObject(i);

                    Note note = new Note();
                    note.setTitle(eachObjectInArray.getString("title"));
                    note.setImage(eachObjectInArray.getString("image"));

                    noteList.add(note);
                } catch (JSONException e) {
                    e.printStackTrace();
                    progressDialog.dismiss();
                }
            }
            adapter.notifyDataSetChanged();
            progressDialog.dismiss();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Volley", error.toString());
            progressDialog.dismiss();
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonArrayRequest);
}
Sarvesh Hon
  • 408
  • 4
  • 15
  • 1
    If my knowledge of JSON serves me correct, subcategory is not a JSON array, but instead it is an object, the JSON Array in this instance is product – beastlyCoder Jun 24 '20 at 03:43
  • the code above is just an example of my code while the json above is actually i want to use for my purpose – Sarvesh Hon Jun 24 '20 at 03:48
  • yes, and in your code, you are saying `jsonArray = jsonObject.getJSONArray("subcategory")` but that is incorrect, because subcategory (in your json) is not an array – beastlyCoder Jun 24 '20 at 03:49
  • thank you, please help me with this and i search a lot for this but found nothing – Sarvesh Hon Jun 24 '20 at 03:53
  • try reading thru this link: https://stackoverflow.com/questions/41928314/how-to-access-response-value-outside-onresponse-method-in-android – beastlyCoder Jun 24 '20 at 04:35

1 Answers1

0

use retrofit instead to fetch the json from the server and convert into the modle class structure list, here is the reference link : https://square.github.io/retrofit/