将多个JSONObject分组到JSONArray中

时间:2017-08-27 05:45:05

标签: java android json

嗨,我有JSON回复

[
    {"sr_no":"1","stn_name":"ALUVA","arrival_time":"5:59:30","distance":"1.81"},
    {"sr_no":"2","stn_name":"PULINCHUDU","arrival_time":"6:02:21","distance":"2.76"},
    {"sr_no":"3","stn_name":"COMPANYPADY","arrival_time":"6:04:19","distance":"3.76"},
    {"sr_no":"4","stn_name":"AMBATTUKAVU","arrival_time":"6:06:15","distance":"4.72"},
    {"sr_no":"5","stn_name":"MUTTOM","arrival_time":"6:08:11","distance":"8.14"},
    {"sr_no":"6","stn_name":"KALAMASSERY","arrival_time":"6:11:20","distance":"8.65"},
    {"sr_no":"7","stn_name":"CUSAT","arrival_time":"6:13:40","distance":"9.15"},
    {"sr_no":"8","stn_name":"PATHADIPALAM","arrival_time":"6:15:52","distance":"12.02"},
    {"sr_no":"9","stn_name":"EDAPPALLY","arrival_time":"6:18:12","distance":"12.55"},
    {"sr_no":"10","stn_name":"CHANGAMPUZHA PARK","arrival_time":"6:20:39","distance":"13.07"},
    {"sr_no":"11","stn_name":"PALARIVATTOM","arrival_time":"6:23:10","distance":"14.43"},
    {"sr_no":"1","stn_name":"ALUVA","arrival_time":"5:59:30","distance":"1.81"},
    {"sr_no":"2","stn_name":"PULINCHUDU","arrival_time":"6:02:21","distance":"2.76"},
    {"sr_no":"3","stn_name":"COMPANYPADY","arrival_time":"6:04:19","distance":"3.76"},
    {"sr_no":"4","stn_name":"AMBATTUKAVU","arrival_time":"6:06:15","distance":"4.72"},
    {"sr_no":"5","stn_name":"MUTTOM","arrival_time":"6:08:11","distance":"8.14"},
    {"sr_no":"6","stn_name":"KALAMASSERY","arrival_time":"6:11:20","distance":"8.65"},
    {"sr_no":"7","stn_name":"CUSAT","arrival_time":"6:13:40","distance":"9.15"},
    {"sr_no":"8","stn_name":"PATHADIPALAM","arrival_time":"6:15:52","distance":"12.02"},
    {"sr_no":"9","stn_name":"EDAPPALLY","arrival_time":"6:18:12","distance":"12.55"},
    {"sr_no":"10","stn_name":"CHANGAMPUZHA PARK","arrival_time":"6:20:39","distance":"13.07"},
    {"sr_no":"11","stn_name":"PALARIVATTOM","arrival_time":"6:23:10","distance":"14.43"}
]

它的所有JSONObject但我想将前11个对象分组到数组中,然后将11个条目分组到另一个数组中,因此响应必须如下:

[
    [
         {"sr_no":"1","stn_name":"ALUVA","arrival_time":"5:59:30","distance":"1.81"},
         {"sr_no":"2","stn_name":"PULINCHUDU","arrival_time":"6:02:21","distance":"2.76"},
         {"sr_no":"3","stn_name":"COMPANYPADY","arrival_time":"6:04:19","distance":"3.76"},
         {"sr_no":"4","stn_name":"AMBATTUKAVU","arrival_time":"6:06:15","distance":"4.72"},{"sr_no":"5","stn_name":"MUTTOM","arrival_time":"6:08:11","distance":"8.14"},{"sr_no":"6","stn_name":"KALAMASSERY","arrival_time":"6:11:20","distance":"8.65"},{"sr_no":"7","stn_name":"CUSAT","arrival_time":"6:13:40","distance":"9.15"},{"sr_no":"8","stn_name":"PATHADIPALAM","arrival_time":"6:15:52","distance":"12.02"},{"sr_no":"9","stn_name":"EDAPPALLY","arrival_time":"6:18:12","distance":"12.55"},{"sr_no":"10","stn_name":"CHANGAMPUZHA PARK","arrival_time":"6:20:39","distance":"13.07"},{"sr_no":"11","stn_name":"PALARIVATTOM","arrival_time":"6:23:10","distance":"14.43"}
    ],
    [{"sr_no":"1","stn_name":"ALUVA","arrival_time":"5:59:30","distance":"1.81"},{"sr_no":"2","stn_name":"PULINCHUDU","arrival_time":"6:02:21","distance":"2.76"},{"sr_no":"3","stn_name":"COMPANYPADY","arrival_time":"6:04:19","distance":"3.76"},{"sr_no":"4","stn_name":"AMBATTUKAVU","arrival_time":"6:06:15","distance":"4.72"},{"sr_no":"5","stn_name":"MUTTOM","arrival_time":"6:08:11","distance":"8.14"},{"sr_no":"6","stn_name":"KALAMASSERY","arrival_time":"6:11:20","distance":"8.65"},{"sr_no":"7","stn_name":"CUSAT","arrival_time":"6:13:40","distance":"9.15"},{"sr_no":"8","stn_name":"PATHADIPALAM","arrival_time":"6:15:52","distance":"12.02"},{"sr_no":"9","stn_name":"EDAPPALLY","arrival_time":"6:18:12","distance":"12.55"},{"sr_no":"10","stn_name":"CHANGAMPUZHA PARK","arrival_time":"6:20:39","distance":"13.07"},{"sr_no":"11","stn_name":"PALARIVATTOM","arrival_time":"6:23:10","distance":"14.43"}]]

如何做到这一点,因为我有超过900条记录。在上面我只显示了2.任何建议将不胜感激,提前致谢。我正在为Android应用程序执行此操作

2 个答案:

答案 0 :(得分:2)

解析原始JSON数组字符串,然后将包含最多11个对象的新JSON数组添加到新数组中,将每个此类数组添加到另一个包含外部的数组:

// String input contains your string input JSON array
JSONArray jArray = new JSONArray(input);   // your original JSON array
JSONArray outArray = new JSONArray();      // the output array of arrays
JSONArray temp = new JSONArray();          // holder for each internal array
for (int i=0; i < jArray.length(); i++) {
    if (i % 11 == 0 && i > 0) {
        outArray.put(temp);
        temp = new JSONArray();
    }
    temp.put(jArray.getJSONObject(i));
}

答案 1 :(得分:0)

首先将json数组读入java对象数组,然后分解为您的大小数组。我更喜欢使用杰克逊框架。 http://javareferencegv.blogspot.com/2015/04/parse-json-into-multiple-java-pojos.html?m=1