使用循环使用Jackson创建Json-string

我有三个String[][]可以不同长度的数组。第二长度始终是固定的,长度为6,但是第一长度在0和之间可以不同6。


我想创建一个包含所有这些String数组的数据的Json字符串。到目前为止,它已经过硬编码了,但是我意识到一旦长度不再固定,我就会陷入麻烦...


travelgl1.put("Duration", str1[0][0]);

travelgl1.put("Walking time", str1[0][1]);

travelgl1.put("Direction", str1[0][2]);

travelgl1.put("Departure", str1[0][3]);

travelgl1.put("Arrival", str1[0][4]);

travelgl1.put("End station", str1[0][5]);

所以基本上我想要一个动态循环,循环遍历String数组并添加该数组中的所有数据。


有没有简单的方法可以做到这一点?我希望所有三个json对象(来自三个String数组)最后都被编译成一个大json字符串。


我希望最终结果看起来像这样:


[{“ String array 1”:[{“ Duration”:“ 33”,“ Walking time”:“ 8”,“ Direction”:“Åkeshov”,“ Departure”:“ 09:39”,“ Arrival”:“ 10:43“,”终点站“:” Sollentuna“},{”持续时间“:” 37“,”步行时间“:” 8“,”方向“:” Alvik“,”出发“:” 09:43“ ,“到达”:“ 10:51”,“终点站”:“ Sollentuna”},{“持续时间”:“ 34”,“步行时间”:“ 8”,“方向”:“ Alvik”,“出发” :“ 09:53”,“到达”:“ 10:58”,“终点站”:“ Sollentuna”},{“持续时间”:“36“,”步行时间“:” 8“,”方向“:”Åkeshov“,”出发“:” 09:59“,”到达“:” 11:06“,”终点站“:” Sollentuna“}, {“ Duration”:“ 33”,“ Walking time”:“ 8”,“ Direction”:“Åkeshov”,“ Departure”:“ 10:09”,“ Arrival”:“ 11:13”}]},{ “字符串数组2”:[{“持续时间”:“ 54”,“步行时间”:“ 13”,“方向”:“ Farsta链”,“出发”:“ 09:43”,“到达”:“ 11 :13“,”终点站“:” Sollentuna“},{”持续时间“:” 47“,”步行时间“:” 13“,”方向”:“ Gullmarsplan”,“出发”:“ 09:50”,“到达”:“ 11:13”,“终点站”:“ Sollentuna”},{“持续时间”:“ 45”,“步行时间” :“ 13”,“方向”:“ Gullmarsplan”,“出发”:“ 10:00”,“到达”:“ 11:21”,“终点站”:“ Sollentuna”},{“持续时间”:“ 42 ”,“步行时间”:“ 13”,“方向”:“ Gullmarsplan”,“出发”:“ 10:10”,“到达”:“ 11:28”,“终点站”:“ Sollentuna”},{ “持续时间”:“ 45”,“步行时间”:“ 13”,“方向”:“ Gullmarsplan”,“出发”:“ 09:30”,“到达”:“ 10:51”,“终点站”:“ Sollentuna”}}},{“字符串数组3”:[{“持续时间”:“ 31”,“步行时间”:“ 12”,“方向”:“赫塞尔比链”,“出发”:“ 09:45”,“到达”:“ 10:51”,“终点站”:“ Sollentuna”},{“持续时间” :“ 31”,“步行时间”:“ 12”,“方向”:“ Alvik”,“出发”:“ 09:52”,“到达”:“ 10:58”,“终点站”:


因此,我真正想要的是根据字符串数组的长度动态创建Json字符串的方法。



阿波罗的战车
浏览 189回答 1
1回答

MM们

请参考以下代码。public List<Map<String, String>> myfunction(String str1[][]) {&nbsp; &nbsp; List<Map<String, String>> travelgl1 = new ArrayList<Map<String, String>>();&nbsp; &nbsp; for (int i = 0; i < str1.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> map = new HashMap<String, String>();&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < 6; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put("Duration", str1[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (j == 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put("Walking time", str1[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (j == 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put("Direction", str1[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (j == 3)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put("Departure", str1[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (j == 4)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put("Arrival", str1[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (j == 5)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put("End station", str1[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; travelgl1.add(map);&nbsp; &nbsp; }&nbsp; &nbsp; return travelgl1;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java