Skip to content Skip to sidebar Skip to footer

Fetching Json Objects And Array Using Volley

I am trying to fetch the below JSONArray objects. There are 2 items but with the loop I have implemented, I am able to fetch only 1 item. I have used 2 for loops. How to resolve it

Solution 1:

Try this it works for me. I just gave the complete architecture print or use the value you wanted as your requirement, for a sample I Logged few values.

The major difference is I used

JSONObject listObj1=newJSONObject(jsonArray.get(i).toString());

instead of your line

JSONObject listObj1=newjsonArray.getJSONObject(i);

The complete code skeleton is below. I used your JSON in a String instead taking from a response, try it if it works then change it to a response. It works well for me.

String response="{'status':200,'list':[{'quot_uid':'QUOTE2018@1','id':'1','expiry_date':'2018-05-29','created_at':'2018-05-2211:45:58','left_days':'9','items':[{'ITEM_NAME':'CopperWires','UNIT':'MT','qty':'5','make':null},{'ITEM_NAME':'OFCCables','UNIT':'MT','qty':'2','make':null}]}]}";
    JSONObject jsonObject = null;
    try {
        jsonObject = newJSONObject(response);

        String status=jsonObject.get("status").toString();
        Log.e("Status is ",status);
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        for(int i=0;i<jsonArray.length();i++){

            //In this loop, you will parse all the array elements inside list arrayJSONObject listObj1=newJSONObject(jsonArray.get(i).toString());

            String qoutid= listObj1.getString("quot_uid");

            Log.e("quot uid is ",qoutid);

            JSONArray lisItems=listObj1.getJSONArray("items");


            for(int j=0;j<lisItems.length();j++){


                JSONObject innerObj=newJSONObject(lisItems.get(j).toString());

                StringITEM_NAME=innerObj.getString("ITEM_NAME");

                Log.e("TAG",ITEM_NAME);

            }

        }


    } catch (JSONException e) {
        Log.e("TAG","Error "+e.toString());
        e.printStackTrace();
    }

check error log as well because it will return JSON parse error any

Post a Comment for "Fetching Json Objects And Array Using Volley"