The single json sample object looks like this:
"Events":[{
"sport_event_id": "sr:sport_event:27636100",
"start_date": "2021-06-22T18:00:00+00:00",
"sport_name": "Soccer",
"competition_name": "UEFA Champions League",
"competition_id": "sr:competition:7",
"season_name": "UEFA Champions League 21/22",
"competitors": [
{
"id": "sr:competitor:37863",
"name": "SS Folgore Falciano Calcio",
"country": "San Marino",
"country_code": "SMR",
"abbreviation": "FFC",
"qualifier": "home",
"gender": "male"
},
{
"id": "sr:competitor:277829",
"name": "FC Prishtina",
"country": "Kosovo",
"country_code": "KOS",
"abbreviation": "PRI",
"qualifier": "away",
"gender": "male"
}
],
"venue": {
"id": "sr:venue:8329",
"name": "Elbasan Arena",
"capacity": 12500,
"city_name": "Elbasan",
"country_name": "Albania",
"map_coordinates": "41.115875,20.091992",
"country_code": "ALB"
},
"probability_home_team_winner": 2.5,
"probability_draw": 88.1,
"probability_away_team_winner": 9.4
},
Please help me understand how to do it. I assume I need to use the Gson library and HashMaps.
I watched a couple of videos on Youtube on how to do a map for a json object. But all I could find is for simple objects with two strings. I can’t get around on how to do it for a more complex json file like this. Would be very thankful if somebody posted a code example for this file.
2
Answers
There are a few libraries available in
java
that can be used to convertjson
to aMap
.json
and convert it to aMap
with the following code:You can read more about that approach here.
You can read more about that approach right here
I would use a site like this to generate Java classes from your sample JSON (see below). Then use Jackson objectMapper to deserialise as follows:
Then you can do stuff like this to get the name of first competitor in the first event (SS Folgore Falciano Calcio):
Generated class structure. You probably should make the fields private and add getters.