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

标签 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"
             }
          ]
       }
    ]
    

最佳答案

你只是为了让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 详细信息 = object.getJSONArray("详细信息");

所以我不明白: JSONObject详细信息=详细信息.getJSONObject(i);

从这一行更改您的代码: JSONArray 详细信息 = object.getJSONArray("详细信息");

喜欢:

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 ...

    }
}

关于java - 我已将 jsonObject 转换为 JsonArray 我如何循环它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45388385/

相关文章:

java - 每个语言环境的 JSF 2 中的不同 facelets(用于模板)

android - 我可以在 android :imeOptions ="actionSearch"? 上写我自己的事件吗

java - 如何在 BottomSheetDialogFragment 内的 TextView 上设置文本

java - 它没有捕获我的存储过程 Spring Boot 的错误

jquery - 火狐浏览器中未返回fineuploader json响应

java - 如何使用正则表达式在 Intellij IDEA 中用小写替换大写?

java - 用于测试 REST Web 应用程序的 JBehave

Java递归斐波那契值

android - 自定义 Gradle 任务以将解析后的 Json 保存到文件中。安卓

java - jackson :属性(property)秩序与继承