Jefferey John Smith
Jefferey John Smith

Reputation: 199

Java how to add JSON List to a class

I am trying to map the JSON i receive from an API EndPoint to a class I have created, but when I use get commands I am getting null. I'm not sure why? I don't think that I have mapped the class correctly but with the layout of the JSON received I'm not quite sure how to map it out correctly. The JSON I will receive will be in the format of:

{1 item
 "matches":[10 items
  0:{3 items
   "when":"Friday, Aug 09 2019 20:00"
    "team1":{1 item
    "teamName":"Liverpool"
              }
    "team2":{1 item
         "teamName":"Norwich"
            }
           }
      1:{3 items
   "when":"Saturday, Aug 10 2019 12:30"
 "team1":{1 item
 "teamName":"West Ham"
 }
   "team2":{1 item
  "teamName":"Manchester City"
    }
 }
  2:{3 items
   "when":"Saturday, Aug 10 2019 15:00"
    "team1":{1 item
      "teamName":"Bournemouth"
      }
      "team2":{1 item
      "teamName":"Sheffield United"
      }
    }

The current class that I have created is this:

@JsonIgnoreProperties
public class Matches {
private String when;
private String referee;
private List<Matches> team1;
private List<Matches> team2;
private String time;
private String venue;
private String attendance;
public String getWhen() {
    return when;
}
public void setWhen(String when) {
    this.when = when;
}
public List<Matches> getTeam1() {
    return team1;
}
public void setTeam1(List<Matches> team1) {
    this.team1 = team1;
}
public List<Matches> getTeam2() {
    return team2;
}
public void setTeam2(List<Matches> team2) {
    this.team2 = team2;
}}

And the Controller code:

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
    .url("URL")
    .get()
    .addHeader("x-rapidapi-key", "KEY")
    .addHeader("x-rapidapi-host", "HOST")
    .build();

Response response = client.newCall(request).execute();
String responseBodyString = response.body().string();
System.out.println(responseBodyString);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Matches bean = objectMapper.readValue(responseBodyString, Matches.class);
System.out.println("Name: " +bean.getWhen());

Does anyone quite know how I can map this JSON to my class, so I can use the data it pulls? Cheers Jeff

Upvotes: 0

Views: 22

Answers (1)

Akif Hadziabdic
Akif Hadziabdic

Reputation: 2890

Json:

{
   "matches":[
      {
         "when":"Friday, Aug 09 2019 20:00",
         "team1":{
            "teamName":"Liverpool"
         },
         "team2":{
            "teamName":"Norwich"
         }
      },
      {
         "when":"Saturday, Aug 10 2019 12:30",
         "team1":{
            "teamName":"West Ham"
         },
         "team2":{
            "teamName":"Manchester City"
         }
      },
      {
         "when":"Saturday, Aug 10 2019 15:00",
         "team1":{
            "teamName":"Bournemouth"
         },
         "team2":{
            "teamName":"Sheffield United"
         }
      }
}

Java:

class Team {
    String teamName;
}
class Match {
    Date when;
    Team team1;
    Team team2;
}
class Response {
    List<Match> matches;
}

Response bean = objectMapper.readValue(responseBodyString, Response.class);

Upvotes: 1

Related Questions