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
question from:
https://stackoverflow.com/questions/66049084/java-how-to-add-json-list-to-a-class 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…