Home > database >  How do I make a Data model to map the json objects in java?
How do I make a Data model to map the json objects in java?

Time:01-22

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.

CodePudding user response:

There are a few libraries available in java that can be used to convert json to a Map.

  1. The first popular library is Gson, which can be used to parse json and convert it to a Map with the following code:
import com.google.gson.Gson;
import java.util.Map;

public static void convert(){
    String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
    Gson gson = new Gson();
    Map<String, String> map = gson.fromJson(json, Map.class);
    ...
} 

You can read more about that approach here.

  1. Another popular library is Jackson:
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;

public static void convert(){
    String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
    ObjectMapper objectMapper = new ObjectMapper();
    Map<String, String> map = objectMapper.readValue(json, Map.class);
    ...
}

You can read more about that approach right here

CodePudding user response:

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:

Root root = objectMapper.readValue(myJsonString, Root.class); */

Then you can do stuff like this to get the name of first competitor in the first event (SS Folgore Falciano Calcio):

root.get(0).competitors.get(0).name

Generated class structure. You probably should make the fields private and add getters.

public class Root{
    @JsonProperty("Events") 
    public ArrayList<Event> events;
}

public class Competitor{
    public String id;
    public String name;
    public String country;
    public String country_code;
    public String abbreviation;
    public String qualifier;
    public String gender;
}

public class Event{
    public String sport_event_id;
    public Date start_date;
    public String sport_name;
    public String competition_name;
    public String competition_id;
    public String season_name;
    public ArrayList<Competitor> competitors;
    public Venue venue;
    public double probability_home_team_winner;
    public double probability_draw;
    public double probability_away_team_winner;
}

public class Venue{
    public String id;
    public String name;
    public int capacity;
    public String city_name;
    public String country_name;
    public String map_coordinates;
    public String country_code;
}
  • Related