Skip to content Skip to sidebar Skip to footer

Fetch Data From Json Category Wise

I am writing an app in which i am fetching data from JSON, and i am able to fetch list of Categories but whenever i do click on any of the Category not getting List of Products und

Solution 1:

You read the JSON from the webservice into a JSONObject however the JSON you have posted is a JSONArray. Try something like this to parse your JSON...

            JSONArray jObj = new JSONArray(json);
            if (jObj != null) {
                for(int i = 0; i < jObj.length(); i++) {
                   JSONObject cat = jObj.getJSONObject(i);
                   String id = cat.getString("categoryId");
                   String title = cat.getString("categoryTitle");
                   JSONArray devices = cat.optJSONArray(title, null);

                   for(int j=0; i<devices.length(); j++) {
                       //get itemId and itemTitle
                   }
                }
            }

Post a Comment for "Fetch Data From Json Category Wise"