我很难将json分组到日期特定组并进一步将结果分组到特定字段
这是json的例子
{
"list":[
{
"category":"League",
"list":[
{
"id":16389,
"matchDesc":"1st Match",
"seriesId":2430,
"seriesDesc":"group A",
"category":"League",
"status":"",
"startDate":1460212200,
"teamAName":"Mumbai Indians",
"teamAShortName":"MUM",
"teamBName":"Rising Pune Supergiants",
"teamBShortName":"RPS",
"teamAImageId":0,
"teamBImageId":0,
"isPrevDay":false
},
{
"id":16390,
"matchDesc":"2nd Match",
"seriesId":2430,
"seriesDesc":"group A",
"category":"League",
"status":"",
"startDate":1460298600,
"teamAName":"Kolkata Knight Riders",
"teamAShortName":"KOL",
"teamBName":"Delhi Daredevils",
"teamBShortName":"DEL",
"teamAImageId":0,
"teamBImageId":146957,
"isPrevDay":false
},
{
"id":16391,
"matchDesc":"3rd Match",
"seriesId":2430,
"seriesDesc":"group B",
"category":"League",
"status":"",
"startDate":1460385000,
"teamAName":"Kings XI Punjab",
"teamAShortName":"MOH",
"teamBName":"Gujarat Lions",
"teamBShortName":"GL",
"teamAImageId":0,
"teamBImageId":0,
"isPrevDay":false
},
{
"id":16085,
"matchDesc":"South Group",
"seriesId":2422,
"seriesDesc":"group B",
"category":"League",
"status":"",
"startDate":1463702400,
"teamAName":"Essex",
"teamAShortName":"Ess",
"teamBName":"Surrey",
"teamBShortName":"Sur",
"teamAImageId":0,
"teamBImageId":0,
"isPrevDay":false
},
{
"id":16086,
"matchDesc":"South Group",
"seriesId":2422,
"seriesDesc":"group C",
"category":"League",
"status":"",
"startDate":1463702400,
"teamAName":"Gloucestershire",
"teamAShortName":"Gloucs",
"teamBName":"Sussex",
"teamBShortName":"SUS",
"teamAImageId":0,
"teamBImageId":0,
"isPrevDay":false
},
{
"id":16087,
"matchDesc":"South Group",
"seriesId":2422,
"seriesDesc":"group C",
"category":"League",
"status":"",
"startDate":1463702400,
"teamAName":"Kent",
"teamAShortName":"KENT",
"teamBName":"Somerset",
"teamBShortName":"SOM",
"teamAImageId":0,
"teamBImageId":0,
"isPrevDay":false
},
{
"id":16412,
"matchDesc":"24th Match",
"seriesId":2430,
"seriesDesc":"group B",
"category":"League",
"status":"",
"startDate":1461853800,
"teamAName":"Mumbai Indians",
"teamAShortName":"MUM",
"teamBName":"Kolkata Knight Riders",
"teamBShortName":"KOL",
"teamAImageId":0,
"teamBImageId":0,
"isPrevDay":false
},
{
"id":16413,
"matchDesc":"25th Match",
"seriesId":2430,
"seriesDesc":"group B",
"category":"League",
"status":"",
"startDate":1461940200,
"teamAName":"Rising Pune Supergiants",
"teamAShortName":"RPS",
"teamBName":"Gujarat Lions",
"teamBShortName":"GL",
"teamAImageId":0,
"teamBImageId":0,
"isPrevDay":false
},
{
"id":16414,
"matchDesc":"26th Match",
"seriesId":2430,
"seriesDesc":"group A",
"category":"League",
"status":"",
"startDate":1462012200,
"teamAName":"Delhi Daredevils",
"teamAShortName":"DEL",
"teamBName":"Kolkata Knight Riders",
"teamBShortName":"KOL",
"teamAImageId":146957,
"teamBImageId":0,
"isPrevDay":false
},
{
"id":16440,
"matchDesc":"52nd Match",
"seriesId":2430,
"seriesDesc":"group A",
"category":"League",
"status":"",
"startDate":1463754600,
"teamAName":"Delhi Daredevils",
"teamAShortName":"DEL",
"teamBName":"Sunrisers Hyderabad",
"teamBShortName":"HYD",
"teamAImageId":146957,
"teamBImageId":0,
"isPrevDay":false
}
]
}
]
}
注意:我已经将json转换为gson,以便我可以使用pojo进行迭代但是我在分组时遇到困难
我希望输出为按日期排列的匹配项,并按“seriesDesc”字段分组
谢谢
编辑: 我期待这个o / p比喻这个
11/08/2016 (considering system time and comparing with timeStamp of object 'startDate')
group A (Considering object 'seriesDesc')
match1
match2
groupB
match1
match2
groupC
match1
match2
match3
12/08/2016
group A
match1
match2
groupB
match1
match2
groupC
match1
match2
match3
注意:match = { “ID”:16389, “matchDesc”:“第一场比赛”, “seriesId”:2430 “seriesDesc”:“A组”, “类别”:“联盟”, “状态”:””, “的startDate”:1460212200, “teamAName”:“孟买印第安人”, “teamAShortName”: “妈妈”, “teamBName”:“崛起的浦那超级巨星”, “teamBShortName”: “RPS”, “teamAImageId”:0, “teamBImageId”:0, “isPrevDay”:假的 }
答案 0 :(得分:1)
这就是我得到的:
String json = ...;
JsonElement el = new Gson().fromJson(json, JsonElement.class);
// Get inner list
JsonArray list = el.getAsJsonObject()
.get("list").getAsJsonArray()
.get(0).getAsJsonObject()
.get("list").getAsJsonArray();
// Make into a regular list, so we can stream
List<JsonObject> objects = new ArrayList<>();
list.forEach(e -> objects.add(e.getAsJsonObject()));
// Group by date, then by series description
Map<LocalDate, Map<String, List<JsonObject>>> result = objects.stream()
.collect(
Collectors.groupingBy(
e -> LocalDateTime.ofInstant(
Instant.ofEpochSecond(e.get("startDate").getAsLong()),
ZoneId.systemDefault()
).toLocalDate(),
Collectors.groupingBy(e -> e.get("seriesDesc").getAsString())
)
);
// Sorted printing
result.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach(date -> {
System.out.println(date.getKey());
date.getValue().entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach(group -> {
System.out.println(group.getKey());
group.getValue().forEach(System.out::println);
System.out.println();
});
});