-3

I have below format json data:

[
  [
    {
      "jsonObject":"jsonObject",
      "jsonObject":"jsonObject",
      "jsonObject":"jsonObject"
    },
    {
      "jsonObject1":"jsonObject1",
      "jsonObject1":"jsonObject1",
      "jsonObject1":"jsonObject1"
    }
  ],
  [
    {
      "jsonObject2":"jsonObject2"
    },
    {
      "jsonObject2":"jsonObject2"
    }
  ]
]

How i get this json format and set into listview using volley i tried below code but i am not getting any object:

{
    JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST, SUBSCRIPTION_Url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    if (response.equals("success")) {
                    } else {
                        try {
                            for (int i = 0; i < response.length(); i++) {

                                JSONArray ja = (JSONArray) response.get(i);
                                JSONObject jb = (JSONObject) ja.get(i);

                                title_array_sub.add(jb.getString("payment_id").toString());
                                notice_array_sub.add(jb.getString("period").toString());
                                title1_array_sub.add(jb.getString("renewal_date").toString());
                                notice1_array_sub.add(jb.getString("auto_renewal").toString());
                                invoice_array_sub.add(jb.getString("invoice").toString());
                                adapter1 = new CustomBaseAdapter(Transaction.this, title_array_sub, notice_array_sub,
                                        title1_array_sub, notice1_array_sub, invoice_array_sub);
                                list2.setAdapter(adapter1);
                            }

                        } catch (JSONException e1) {
                        } catch (ParseException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(KEY_MOVIE_ID, user_id);
                map.put(KEY_USER_ID, User_type);
                Log.d("movie_on_description", String.valueOf(map));
                return map;
            }
    };
    RequestQueue requestqueue = Volley.newRequestQueue(Transaction.this);
    requestqueue.add(request);
}

Please Help i also try without using loop and getting value in single string with 0 index and i am getting value but not getting values while using loop. Thankyou

Akhilesh Dhar Dubey
  • 2,152
  • 2
  • 25
  • 39
Sumit Kumar
  • 643
  • 1
  • 8
  • 19

3 Answers3

1

try this in onSuccess

    if (response.equals("success")) {
            } else {
                try {
                    for (int i = 0; i < response.length(); i++) {

                        JSONArray ja = response.getJsonArray(i);
                        for(int j=0;j<ja.length();j++){  
                            JSONObject jb = ja.getJsonObject(j);

                            title_array_sub.add(jb.getString("payment_id").toString());
                            notice_array_sub.add(jb.getString("period").toString());
                            title1_array_sub.add(jb.getString("renewal_date").toString());
                            notice1_array_sub.add(jb.getString("auto_renewal").toString());
                            invoice_array_sub.add(jb.getString("invoice").toString());
                        }

                    }
    adapter1 = new CustomBaseAdapter(Transaction.this, title_array_sub, notice_array_sub, title1_array_sub, notice1_array_sub, invoice_array_sub);
                        list2.setAdapter(adapter1);
                } catch (JSONException e1) {

                } catch (ParseException e1) {
                    e1.printStackTrace();
                }
            }

and also you can use this link for json parsing

Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13
  • thnx for double loops!!! but i don't understand why i am not getting string also now!! – Sumit Kumar May 01 '17 at 10:02
  • if(jb.has("payment_id")){ title_array_sub.add(jb.getString("payment_id").toString()); } please check this condition is true or false – Bhupat Bheda May 01 '17 at 10:06
  • 1
    Your json response is not a right way json response like {"array":[{"a":"avalue"},{"b":"bvalue"}]}.json string always starting with json object not json array – Bhupat Bheda May 01 '17 at 10:14
  • yes i think also thats a issue in json format, because i am not that bad in Json parsing !! thnx – Sumit Kumar May 01 '17 at 10:19
  • Glad it worked.Please accept my answer.You can also visit : https://stackoverflow.com/tour to get more information about SO and support me as much as you can. – Bhupat Bheda May 01 '17 at 10:21
0

You keep making a new Adapter in your for loop and setting it to your listview. Move the creating and assigning outside of your for loop.

for (int i = 0; i < response.length(); i++) {   
    JSONArray ja= (JSONArray) response.get(i);
    JSONObject jb = (JSONObject) ja.get(i);

    title_array_sub.add(jb.getString("payment_id").toString());
    notice_array_sub.add(jb.getString("period").toString());
    title1_array_sub.add(jb.getString("renewal_date").toString());
    notice1_array_sub.add(jb.getString("auto_renewal").toString());
    invoice_array_sub.add(jb.getString("invoice").toString());      
}
adapter1 = new CustomBaseAdapter(Transaction.this, title_array_sub, notice_array_sub, title1_array_sub, notice1_array_sub,invoice_array_sub);
list2.setAdapter(adapter1);

Your response consists of more JSONArrays, to retrieve these use response.getJSONArray(Integer.valueOf(i))

Your JSONArrays consists of JSONObjects, you can loop through these objects again with

for (int i = 0; i < array.length(); i++) {
    JSONObject obj = array.getJSONObject(i);
    // do something
}

Look here for more examples: JSON Array iteration in Android/Java

Community
  • 1
  • 1
Denny
  • 1,766
  • 3
  • 17
  • 37
0

It will be classified by two arrays 1) Array[0] and Array[1] . So in order of getting these json data i done this way:

         StringRequest strRequest = new StringRequest(Request.Method.POST, SUBSCRIPTION_Url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {

                                Log.d("responsetrans_sub",response);

                                if (response.trim().equals("success")) {

                                } else {

                                    try {
                                        JSONArray jsonObj = new JSONArray(response);

                                        JSONArray contacts =  jsonObj.getJSONArray(0);
                                        JSONArray contacts1 =  jsonObj.getJSONArray(1);
                                        for (int i = 0; i < contacts.length(); i++) {
                                            JSONObject contacting = contacts.getJSONObject(i);
                                            title_array_sub.add(contacting.getString("payment_id").toString());
                                            notice_array_sub.add(contacting.getString("period").toString());
                                            notice1_array_sub.add(contacting.getString("auto_renewal").toString());
                                            ivoice_array_sub.add(contacting.getString("invoice").toString());

                                        }
                                        for (int i = 0; i < contacts1.length(); i++) {

                                            JSONObject contacting1 = contacts1.getJSONObject(i);
                                            title1_array_sub.add(contacting1.getString("renewal_date").toString());


                                            adapter1 = new CustomBaseAdapter(Transaction.this, title_array_sub, notice_array_sub,title1_array_sub, notice1_array_sub,ivoice_array_sub);
                                            list_subscription.setAdapter(adapter1);

                                        }

                                    } catch (JSONException e1) {

                                    } catch (ParseException e1) {
                                        e1.printStackTrace();
                                    }

                                }
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {

                            }
                        }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> map = new HashMap<String, String>();
                        map.put(KEY_USER_ID, user_id);
                        map.put(KEY_USER_TYPE, User_type);

                        return map;
                    }
                };
                RequestQueue requestqueue = Volley.newRequestQueue(Transaction.this);
                requestqueue.add(strRequest);**
Sumit Kumar
  • 643
  • 1
  • 8
  • 19