我已将我的jsonObject转换为JsonArray如何循环它

时间:2017-07-29 10:51:47

标签: java android json

  1. 我正在尝试转换我的jsonArray,我已经成功获得了 数据但如何循环我的jsonArray所以我可以得到完整的细节 在listview中。如果有多个数据,我如何附加我的数据 在同一个对象中并将其解析为listview。
  2. 我已将jsonObject转换为Jsonarray

    public class getData extends AsyncTask<String, String, String> {
    
        @Override
        protected String doInBackground(String... params) {
            return JsonParser.makeHttpUrlConnectionRequest(url, getParam());
        }
    
        private List<NameValuePair> getParam() {
            SharedPreferences preferences = ReportActivity.this.getSharedPreferences(getString(R.string.detail), MODE_PRIVATE);
            enroll_no = preferences.getString(getString(R.string.loginenroll), "");
    
            List<NameValuePair> list = new ArrayList<>();
            list.add(new BasicNameValuePair("enroll_no", enroll_no));
    
            // Log.e("List", String.valueOf(list.add(new BasicNameValuePair("enroll_no",enroll))));
            Log.e("Request in Profile", String.valueOf(list));
            return list;
        }
    
        @Override
        protected void onPostExecute(String httpresponse) {
            super.onPostExecute(httpresponse);
            if (httpresponse == null) {
                Toast.makeText(ReportActivity.this, "Unable to Connect", Toast.LENGTH_SHORT).show();
            } else if (httpresponse.equals("error")) {
                Toast.makeText(ReportActivity.this, "Internal Server Error", Toast.LENGTH_SHORT).show();
    
            } else {
    
                try {
                    responseParameter = new JSONObject(httpresponse);
                    JSONObject fees_history = responseParameter.getJSONObject("fees_history");
    
                    //  JSONObject RC1 = fees_history.getJSONObject("RC1");
                    Iterator rc1 = fees_history.keys();
                    JSONArray jsonArray = new JSONArray();
                    int i = 0;
                    while (rc1.hasNext()) {
                        String key = (String) rc1.next();
                        jsonArray.put(fees_history.get(key));
    
                        JSONObject object = jsonArray.getJSONObject(i++);
                        JSONObject heading = object.getJSONObject("heading");
                        JSONArray details = object.getJSONArray("details");
                        Log.e("details", details.toString());
                        String Amount = heading.getString("Amount");
                        String date = heading.getString("date");
                        String Reciept = heading.getString("Reciept");
    
                      //  for (int j = 0; j < details.length(); j++) {
    
                            JSONObject detail = details.getJSONObject(i);
    
                            String Month = detail.getString("Month");
    
                            String Amountt = detail.getString("Amount");
                            Log.e("Amount", Amountt);
                            Log.e("Month", Month);
    
                            Unpaid_fees_detail unpaid_fees_detail = new Unpaid_fees_detail(Reciept, date, Amount,Amountt,Month);
                            unpaid_fees_details.add(unpaid_fees_detail);
                        }
    
                    } catch (JSONException e) {
                    e.printStackTrace();
                }
    
                //  Log.e("RC1",RC1.toString());
    
                 //   Log.e("fees_history", fees_history.toString());
    
                }
                Unpaid_fees_detail_adapter unpaid_fees_detail_adapter = new Unpaid_fees_detail_adapter(ReportActivity.this, unpaid_fees_details);
                listView.setAdapter(unpaid_fees_detail_adapter);
            }
    
        }
    

    这是我的JsonArray

    [  
       {  
          "heading":{  
             "Amount":11800,
             "Reciept":"RC1",
             "date":"29-07-2017"
          },
          "details":[  
             {  
                "unique_id":"75",
                "Reciept":"RC1",
                "Gr_num":"1355",
                "Amount":"100",
                "bal":"0",
                "Month":"Aug",
                "year":"2017",
                "fee_type":"Bus Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"yes",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-06-30",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-06-30 14:08:30",
                "updated_at":"2017-06-30 14:08:30"
             },
             {  
                "unique_id":"65",
                "Reciept":"RC1",
                "Gr_num":"1355",
                "Amount":"1000",
                "bal":"0",
                "Month":"Aug",
                "year":"2017",
                "fee_type":"Monthly Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"no",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-07-29",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-07-29 12:20:52",
                "updated_at":"2017-07-29 12:20:52"
             },
             {  
                "unique_id":"65",
                "Reciept":"RC1",
                "Gr_num":"1355",
                "Amount":"100",
                "bal":"0",
                "Month":"Dec",
                "year":"2017",
                "fee_type":"Bus Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"no",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-07-29",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-07-29 12:20:52",
                "updated_at":"2017-07-29 12:20:52"
             },
             {  
                "unique_id":"65",
                "Reciept":"RC1",
                "Gr_num":"1355",
                "Amount":"1000",
                "bal":"0",
                "Month":"Dec",
                "year":"2017",
                "fee_type":"Monthly Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"no",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-07-29",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-07-29 12:21:12",
                "updated_at":"2017-07-29 12:21:12"
             },
             {  
                "unique_id":"65",
                "Reciept":"RC1",
                "Gr_num":"1355",
                "Amount":"100",
                "bal":"0",
                "Month":"Feb",
                "year":"2017",
                "fee_type":"Bus Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"no",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-07-29",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-07-29 12:21:12",
                "updated_at":"2017-07-29 12:21:12"
             },
             {  
                "unique_id":"75",
                "Reciept":"RC1",
                "Gr_num":"1355",
                "Amount":"1000",
                "bal":"0",
                "Month":"Term 1",
                "year":"2017",
                "fee_type":"Monthly Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"no",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-06-30",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-06-30 14:08:30",
                "updated_at":"2017-06-30 14:08:30"
             },
             {  
                "unique_id":"75",
                "Reciept":"RC1",
                "Gr_num":"1355",
                "Amount":"7000",
                "bal":"3000",
                "Month":"Admission Fee",
                "year":"2017",
                "fee_type":"One time",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"no",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-06-30",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-06-30 14:08:30",
                "updated_at":"2017-06-30 14:08:30"
             },
             {  
                "unique_id":"65",
                "Reciept":"RC1",
                "Gr_num":"1355",
                "Amount":"1500",
                "bal":"1500",
                "Month":"Admission Fee",
                "year":"2017",
                "fee_type":"One time",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"no",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-07-29",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-07-29 12:20:52",
                "updated_at":"2017-07-29 12:20:52"
             }
          ]
       },
       {  
          "heading":{  
             "Amount":200,
             "Reciept":"RC2",
             "date":"30-06-2017"
          },
          "details":[  
             {  
                "unique_id":"65",
                "Reciept":"RC2",
                "Gr_num":"1355",
                "Amount":"100",
                "bal":"0",
                "Month":"Jan",
                "year":"2017",
                "fee_type":"Bus Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"no",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-07-29",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-07-29 12:20:52",
                "updated_at":"2017-07-29 12:20:52"
             },
             {  
                "unique_id":"75",
                "Reciept":"RC2",
                "Gr_num":"1355",
                "Amount":"100",
                "bal":"0",
                "Month":"Jul",
                "year":"2017",
                "fee_type":"Bus Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"yes",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-06-30",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-06-30 14:08:30",
                "updated_at":"2017-06-30 14:08:30"
             }
          ]
       },
       {  
          "heading":{  
             "Amount":1100,
             "Reciept":"RC3",
             "date":"30-06-2017"
          },
          "details":[  
             {  
                "unique_id":"75",
                "Reciept":"RC3",
                "Gr_num":"1355",
                "Amount":"1000",
                "bal":"0",
                "Month":"Jul",
                "year":"2017",
                "fee_type":"Monthly Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"no",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-06-30",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-06-30 14:08:30",
                "updated_at":"2017-06-30 14:08:30"
             },
             {  
                "unique_id":"75",
                "Reciept":"RC3",
                "Gr_num":"1355",
                "Amount":"100",
                "bal":"0",
                "Month":"Jun",
                "year":"2017",
                "fee_type":"Bus Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"yes",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-06-30",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-06-30 14:08:30",
                "updated_at":"2017-06-30 14:08:30"
             }
          ]
       },
       {  
          "heading":{  
             "Amount":2100,
             "Reciept":"RC4",
             "date":"29-07-2017"
          },
          "details":[  
             {  
                "unique_id":"65",
                "Reciept":"RC4",
                "Gr_num":"1355",
                "Amount":"1000",
                "bal":"0",
                "Month":"Jun",
                "year":"2017",
                "fee_type":"Monthly Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"no",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-07-29",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-07-29 12:20:52",
                "updated_at":"2017-07-29 12:20:52"
             },
             {  
                "unique_id":"65",
                "Reciept":"RC4",
                "Gr_num":"1355",
                "Amount":"100",
                "bal":"0",
                "Month":"Nov",
                "year":"2017",
                "fee_type":"Bus Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"no",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-07-29",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-07-29 12:20:52",
                "updated_at":"2017-07-29 12:20:52"
             },
             {  
                "unique_id":"65",
                "Reciept":"RC4",
                "Gr_num":"1355",
                "Amount":"1000",
                "bal":"0",
                "Month":"Nov",
                "year":"2017",
                "fee_type":"Monthly Fee",
                "pay_mode":"cash",
                "cheque_num":"0",
                "lflag":"no",
                "late_fee":"0",
                "discount":"0",
                "reason":"",
                "date":"2017-07-29",
                "bank_name":"",
                "cheque_date":"0000-00-00",
                "is_cheq_msg_send":"0",
                "is_active":"1",
                "cheq_msg":"",
                "created_at":"2017-07-29 12:21:12",
                "updated_at":"2017-07-29 12:21:12"
             }
          ]
       }
    ]
    

1 个答案:

答案 0 :(得分:1)

你只是要fori循环

ArrayList<DetailItem> detailItems = new ArrayList<>();
JSONArray details = response.getJSONArray("details");
if (details != null) {
    for (int k = 0; k < details.length(); k++) {
        JSONObject jsonObject = details.getJSONObject(k);

        String uniqueId = jsonObject.getString("unique_id");
        String reciept = jsonObject.getString("Reciept");
        // Your Code ...

        DetailItem detailItem = new DetailIte();
        detailItem.setUniqueId(uniqueId);
        detailItem.setReciept(reciept);
        // Your Code ...

        detailItems.add(detailItem);
        // Your Code ...

    }
}

你创造了:     JSONArray details = object.getJSONArray(“details”);

所以我不了解:     JSONObject detail = details.getJSONObject(i);

从此行更改您的代码:     JSONArray details = object.getJSONArray(“details”);

喜欢:

JSONArray details = response.getJSONArray("details");
// List Of Custom Object Class 'DetailItem'
// It Contains fields of details object , unique_id and Reciept and etc
ArrayList<DetailItem> detailItems = new ArrayList<>();
if (details != null) {
    for (int k = 0; k < details.length(); k++) {
        JSONObject jsonObject = details.getJSONObject(k);

        String uniqueId = jsonObject.getString("unique_id");
        String reciept = jsonObject.getString("Reciept");
        // Your Code ...

        DetailItem detailItem = new DetailIte();
        detailItem.setUniqueId(uniqueId);
        detailItem.setReciept(reciept);
        // Your Code ...

        detailItems.add(detailItem);
        // Your Code ...

    }
}