gpt4 book ai didi

java - 将多个 JSONObject 分组到 JSONArray 中

转载 作者:行者123 更新时间:2023-12-02 02:38:20 25 4
gpt4 key购买 nike

您好,我有 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 应用程序执行此操作

最佳答案

解析原始 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));
}

关于java - 将多个 JSONObject 分组到 JSONArray 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45902037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com