Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

spring - 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

question from:https://stackoverflow.com/questions/66049084/java-how-to-add-json-list-to-a-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...