Reputation: 334
I'm having difficulty in grouping json into date specific groups and further grouping the result into specific field
here's the example 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
}
]
}
]
}
Note: I have already converted json into gson so that i could use pojo for iteration but i'm facing difficulty in grouping
i'm expecting output as matches which are scheduled per date and grouped by the 'seriesDesc' field
Thanks
EDIT: i'm expecting the o/p something similler to this
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
Note: match = { "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 }
Upvotes: 0
Views: 1146
Reputation: 33865
This is what I got:
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();
});
});
Upvotes: 1