0

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"
        }
      ]
    ]
  ]
}
Ali Tamoor
  • 896
  • 12
  • 18

3 Answers3

1

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.

D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97
0

1-good knowledge of array and object 2- knowledge about json format 3- if an array keep it inside a collection and object will be part of that collection.

4- https://www.jsonschema2pojo.org/ link may help you on initial stage

JMrl
  • 1
  • 1
0

If you know that the JSON file you receive will always be formatted properly, then you could use Karamveer's response above.

If not, simply use something like Jackson, Gson, json-io, or Genson to parse the JSON file you receive.

Then you could use JavaPoet to generate your classes with the given JSON values.

Mert Ozturk
  • 409
  • 1
  • 5
  • 17