简体   繁体   中英

Map the response(json) to java classes

for list

@JsonIgnoreProperties(ignoreUnknown = true)
public class LolMatch {
    private long gameId;
    private String role;
    private int season;
    private String platformID;
    private int champion;
    private int queue;
    private String lane;
    private long timestamp;

    public long getGameId() {
        return gameId;
    }

    public void setGameId(long gameId) {
        this.gameId = gameId;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public int getSeason() {
        return season;
    }

    public void setSeason(int season) {
        this.season = season;
    }

    public String getPlatformID() {
        return platformID;
    }

    public void setPlatformID(String platformID) {
        this.platformID = platformID;
    }

    public int getChampion() {
        return champion;
    }

    public void setChampion(int champion) {
        this.champion = champion;
    }

    public int getQueue() {
        return queue;
    }

    public void setQueue(int queue) {
        this.queue = queue;
    }

    public String getLane() {
        return lane;
    }

    public void setLane(String lane) {
        this.lane = lane;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

}

for response

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.ArrayList;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class LolMatchList {
    private int startIndex;
    private int totalGames;
    private int endIndex;
    List<LolMatch> matches;



    public LolMatchList() {
        matches = new ArrayList<LolMatch>();
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public int getTotalGames() {
        return totalGames;
    }

    public void setTotalGames(int totalGames) {
        this.totalGames = totalGames;
    }

    public int getEndIndex() {
        return endIndex;
    }

    public void setEndIndex(int endIndex) {
        this.endIndex = endIndex;
    }

    public List<LolMatch> getMatches() {
        return matches;
    }

    public void setMatches(List<LolMatch> matches) {
        this.matches = matches;
    }
}

Example response is like this

{
  "startIndex": 0,
  "totalGames": 151,
  "endIndex": 100,
  "matches": [
    {
      "gameId": 1,
      "role": "DUO",
      "season": 13,
      "platformId": "xx",
      "champion": 13,
      "queue": 450,
      "lane": "MID",
      "timestamp": 1589718112737
    },
    {
      "gameId": 2,
      "role": "SOLO",
      "season": 13,
      "platformId": "xx",
      "champion": 7,
      "queue": 450,
      "lane": "BOTTOM",
      "timestamp": 1589716370234
    },
    {
      "gameId": 11,
      "role": "DUO_SUPPORT",
      "season": 13,
      "platformId": "xx",
      "champion": 55,
      "queue": 450,
      "lane": "MID",
      "timestamp": 1589714562139
    }
  ]
}

This is how I get the response

URL url = new URL(request_url);

BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

JSONObject jAns = new JSONObject(br.readLine());

JSONArray matches = jAns.getJSONArray("matches");

I could not map the response to these classes, LolMatch and LolMatchList. Is there a way to do this automatically or should I parse the data and go throught the response to create the objects myself? Thanks

You can use the Gson to convert Java object to / from JSON.

1 - Download Gson dependency

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
</dependency>

2 - Convert JSON to Java Objects

MyObject mObject = gson.fromJson(jsonString, MyObject.class);

See more in Gson – How to convert Java object to / from JSON

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM