3

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? Is there any other way of fetching all the data? If I start the for loop from -1 then it shows array index out of bounds.

The JSON structure is as below

    {
    "status": 200,
    "list": [
        {
            "quot_uid": "QUOTE2018@1",
            "id": "1",
            "expiry_date": "2018-05-29",
            "created_at": "2018-05-22 11:45:58",
            "left_days": "9",
            "items": [
                {
                    "ITEM_NAME": "Copper Wires",
                    "UNIT": "MT",
                    "qty": "5",
                    "make": null
                },
                {
                    "ITEM_NAME": "OFC Cables",
                    "UNIT": "MT",
                    "qty": "2",
                    "make": null
                }
            ]
        }
    ]
}

And this is the code which I'm already tried

 JSONObject jsonObject = new JSONObject(response);
                        JSONArray jsonArray = jsonObject.getJSONArray("list");
                        tv.removeAllViewsInLayout();
                        int flag = 1;

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                            JSONArray jsonArray1 = jsonObject1.getJSONArray("items");
                            for (int j = 0; j < jsonArray1.length(); j++) {
                            TableRow tr = new TableRow(Main2Activity.this);
                            tr.setLayoutParams(new ViewGroup.LayoutParams(
                                    ViewGroup.LayoutParams.FILL_PARENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT));
                            if (flag == 1) {

                                TextView t1 = new TextView(Main2Activity.this);
                                t1.setPadding(10, 30, 10, 30);
                                t1.setText("ITEM NAME");
                                t1.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                                t1.setTop(20);
                                t1.setTextColor(Color.BLACK);
                                t1.setTextSize(15);
                                t1.setTypeface(null, Typeface.BOLD);
                                tr.addView(t1);

                                TextView t2 = new TextView(Main2Activity.this);
                                t2.setPadding(10, 30, 10, 30);
                                t2.setText("QTY");
                                t2.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                                t2.setTop(20);
                                t2.setTextColor(Color.BLACK);
                                t2.setTextSize(15);
                                t2.setTypeface(null, Typeface.BOLD);
                                tr.addView(t2);

                                tv.addView(tr);
                                final View vline = new View(Main2Activity.this);
                                vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 10));
                                vline.setBackgroundColor(Color.BLUE);
                                tv.addView(vline);
                                flag = 0;
                            } else {
                                JSONObject jsonObject2 = jsonArray1.getJSONObject(j);
                                TextView tv1 = new TextView(Main2Activity.this);
                                tv1.setPadding(5, 30, 5, 30);
                                String item_nm = jsonObject2.getString("ITEM_NAME");
                                tv1.setText(item_nm);
                                tv1.setTextColor(Color.BLACK);
                                tv1.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                                tv1.setTextSize(15);
                                tr.addView(tv1);

                                TextView tv2 = new TextView(Main2Activity.this);
                                tv2.setPadding(5, 30, 5, 30);
                                String item_qty = jsonObject2.getString("qty");
                                tv2.setText(item_qty);
                                tv2.setTextColor(Color.BLACK);
                                tv2.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
                                tv2.setTextSize(15);
                                tr.addView(tv2);

                                tv.addView(tr);
                                final View vline1 = new View(Main2Activity.this);
                                vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                                vline1.setBackgroundColor(Color.rgb(1, 132, 143));
                                tv.addView(vline1);
                            }

1 Answer 1

2

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=new JSONObject(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 = new JSONObject(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 array

            JSONObject listObj1=new JSONObject(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=new JSONObject(lisItems.get(j).toString());

                String ITEM_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

Sign up to request clarification or add additional context in comments.

2 Comments

did you tried my same code copy paste and Check your log its should come like this Status is: 200 quot uid is: QUOTE2018@1 TAG: CopperWires OFCCables
@Aparna put a try catch and log error, I think your code have a JSON error like you are trying to read an invalid key. i will edit my answer copy paste it into an activity and Check your log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.