Skip to content
Advertisement

How can I write Java Model Class for Json Schema?

the problem is with coach, goalkeepers and defender like entity I don’t know how to make a class for this kind of response

{
  "squad": [
    [
      "coach",
      [
        {
          "id": 78621,
          "name": "Gareth Southgate",
          "ccode": "ENG",
          "cname": "England"
        }
      ]
    ],
    [
      "goalkeepers",
      [
        {
          "id": 280287,
          "name": "Sam Johnstone",
          "ccode": "8659",
          "cname": "West Bromwich Albion",
          "role": "goalkeepers"
        },
        {
          "id": 303919,
          "name": "Jordan Pickford",
          "ccode": "8668",
          "cname": "Everton",
          "role": "goalkeepers"
        },
        {
          "id": 746395,
          "name": "Aaron Ramsdale",
          "ccode": "8657",
          "cname": "Sheffield United",
          "role": "goalkeepers"
        }
      ]
    ],
    [
      "defenders",
      [
        {
          "id": 159833,
          "name": "Kyle Walker",
          "ccode": "8456",
          "cname": "Manchester City",
          "role": "defenders"
        },
        {
          "id": 191869,
          "name": "Kieran Trippier",
          "ccode": "9906",
          "cname": "Atletico Madrid",
          "role": "defenders"
        },
        {
          "id": 247761,
          "name": "Conor Coady",
          "ccode": "8602",
          "cname": "Wolverhampton Wanderers",
          "role": "defenders"
        },
        {
          "id": 255610,
          "name": "Harry Maguire",
          "ccode": "10260",
          "cname": "Manchester United",
          "role": "defenders"
        },
        {
          "id": 263653,
          "name": "John Stones",
          "ccode": "8456",
          "cname": "Manchester City",
          "role": "defenders"
        },
        {
          "id": 362694,
          "name": "Luke Shaw",
          "ccode": "10260",
          "cname": "Manchester United",
          "role": "defenders"
        },
        {
          "id": 419664,
          "name": "Tyrone Mings",
          "ccode": "10252",
          "cname": "Aston Villa",
          "role": "defenders"
        },
        {
          "id": 672469,
          "name": "Ben Chilwell",
          "ccode": "8455",
          "cname": "Chelsea",
          "role": "defenders"
        },
        {
          "id": 776151,
          "name": "Ben White",
          "ccode": "10204",
          "cname": "Brighton & Hove Albion",
          "role": "defenders"
        },
        {
          "id": 807729,
          "name": "Reece James",
          "ccode": "8455",
          "cname": "Chelsea",
          "role": "defenders"
        }
      ]
    ],
    [
      "midfielders",
      [
        {
          "id": 156008,
          "name": "Jordan Henderson",
          "ccode": "8650",
          "cname": "Liverpool",
          "role": "midfielders"
        },
        {
          "id": 609755,
          "name": "Kalvin Phillips",
          "ccode": "8463",
          "cname": "Leeds United",
          "role": "midfielders"
        },
        {
          "id": 654096,
          "name": "Declan Rice",
          "ccode": "8654",
          "cname": "West Ham United",
          "role": "midfielders"
        },
        {
          "id": 750032,
          "name": "Mason Mount",
          "ccode": "8455",
          "cname": "Chelsea",
          "role": "midfielders"
        },
        {
          "id": 1077894,
          "name": "Jude Bellingham",
          "ccode": "9789",
          "cname": "Borussia Dortmund",
          "role": "midfielders"
        }
      ]
    ],
    [
      "attackers",
      [
        {
          "id": 194165,
          "name": "Harry Kane",
          "ccode": "8586",
          "cname": "Tottenham Hotspur",
          "role": "attackers"
        },
        {
          "id": 246575,
          "name": "Raheem Sterling",
          "ccode": "8456",
          "cname": "Manchester City",
          "role": "attackers"
        },
        {
          "id": 312765,
          "name": "Jack Grealish",
          "ccode": "10252",
          "cname": "Aston Villa",
          "role": "attackers"
        },
        {
          "id": 612150,
          "name": "Dominic Calvert-Lewin",
          "ccode": "8668",
          "cname": "Everton",
          "role": "attackers"
        },
        {
          "id": 696365,
          "name": "Marcus Rashford",
          "ccode": "10260",
          "cname": "Manchester United",
          "role": "attackers"
        },
        {
          "id": 815006,
          "name": "Phil Foden",
          "ccode": "8456",
          "cname": "Manchester City",
          "role": "attackers"
        },
        {
          "id": 846381,
          "name": "Jadon Sancho",
          "ccode": "9789",
          "cname": "Borussia Dortmund",
          "role": "attackers"
        },
        {
          "id": 961995,
          "name": "Bukayo Saka",
          "ccode": "9825",
          "cname": "Arsenal",
          "role": "attackers"
        }
      ]
    ]
  ]
}

Advertisement

Answer

When creating Java POJO for a json schema you just need to map the fields. For your problem we can something like this.

public class SquadDto{
    List<Squad> squads;
}

public class Squad{
    List<Coach> coach;
    List<Goalkeeper> goalkeepers;
    List<Defender> defenders;
    List<Midfielder> midfielders;
    List<Attacker> attackers;
}
public class Coach{
    long id;
    String name;
    String ccode;
    String cname;
    String role;
}

Similarly we can make classes for GoalKeeper,.. ,so that they map the fields in JSON.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement