簡體   English   中英

如何使用Retrofit2和ArrayList進行解析?

[英]How to parse with Retrofit2 and ArrayList?

我嘗試解析數據,但是我的問題是我的json響應不是以[]開頭。 我使用這個網站( http://www.jsonschema2pojo.org/ )來幫助我,但是如果沒有[],它將無法正常工作...

我的接口API.java

String BASE_URL = "https://statsapi.web.nhl.com/api/v1/people/";

@Headers("Content-Type: application/json")
@GET("8472382/stats?stats=statsSingleSeason&season=20172018")
Call<Players> getData();

這是我要解析的響應:

{
   "stats":[
      {
         "type":{
            "displayName":"statsSingleSeason"
         },
         "splits":[
            {
               "season":"20172018",
               "stat":{
                  "timeOnIce":"1711:02",
                  "assists":10,
                  "goals":3,
                  "pim":21,
                  "shots":93,
                  "games":81,
                  "hits":84,
                  "powerPlayGoals":0,
                  "powerPlayPoints":0,
                  "powerPlayTimeOnIce":"06:17",
                  "evenTimeOnIce":"1442:16",
                  "penaltyMinutes":"21",
                  "faceOffPct":0.0,
                  "shotPct":3.2,
                  "gameWinningGoals":0,
                  "overTimeGoals":0,
                  "shortHandedGoals":0,
                  "shortHandedPoints":0,
                  "shortHandedTimeOnIce":"262:29",
                  "blocked":188,
                  "plusMinus":-9,
                  "points":13,
                  "shifts":2292,
                  "timeOnIcePerGame":"21:07",
                  "evenTimeOnIcePerGame":"17:48",
                  "shortHandedTimeOnIcePerGame":"03:14",
                  "powerPlayTimeOnIcePerGame":"00:04"
               }
            }
         ]
      }
   ]
}

嘗試這個:

String BASE_URL = "https://statsapi.web.nhl.com/api/v1/people/";
    @Headers("Content-Type: application/json")
    @GET("8472382.json")
    Call<Stats> getData();

這些是在stackoverflow上發布的JSON的模型,但是您應該注意Json的URL,因為我嘗試了它,它顯示了另一個json結構。

在不同的Java文件中創建每個模型,然后添加public

class Stats {

    private List<Stat> stats;

    public List<Stat> getStats() {
        return stats;
    }

    public void setStats(List<Stat> stats) {
        this.stats = stats;
    }
}

public class Stat {

    private StatType type;
    private List<Split> splits;

    public StatType getType() {
        return type;
    }

    public void setType(StatType type) {
        this.type = type;
    }

    public List<Split> getSplits() {
        return splits;
    }

    public void setSplits(List<Split> splits) {
        this.splits = splits;
    }
}

class StatType {

    private String displayName;

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }
}

class Split {
    private String season;
    StatDetail StatObject;


    // Getter Methods

    public String getSeason() {
        return season;
    }

    public StatDetail getStat() {
        return StatObject;
    }

    // Setter Methods

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

    public void setStat(StatDetail statObject) {
        this.StatObject = statObject;
    }
}

class StatDetail {
    private String timeOnIce;
    private float assists;
    private float goals;
    private float pim;
    private float shots;
    private float games;
    private float hits;
    private float powerPlayGoals;
    private float powerPlayPoints;
    private String powerPlayTimeOnIce;
    private String evenTimeOnIce;
    private String penaltyMinutes;
    private float faceOffPct;
    private float shotPct;
    private float gameWinningGoals;
    private float overTimeGoals;
    private float shortHandedGoals;
    private float shortHandedPoints;
    private String shortHandedTimeOnIce;
    private float blocked;
    private float plusMinus;
    private float points;
    private float shifts;
    private String timeOnIcePerGame;
    private String evenTimeOnIcePerGame;
    private String shortHandedTimeOnIcePerGame;
    private String powerPlayTimeOnIcePerGame;


    // Getter Methods

    public String getTimeOnIce() {
        return timeOnIce;
    }

    public float getAssists() {
        return assists;
    }

    public float getGoals() {
        return goals;
    }

    public float getPim() {
        return pim;
    }

    public float getShots() {
        return shots;
    }

    public float getGames() {
        return games;
    }

    public float getHits() {
        return hits;
    }

    public float getPowerPlayGoals() {
        return powerPlayGoals;
    }

    public float getPowerPlayPoints() {
        return powerPlayPoints;
    }

    public String getPowerPlayTimeOnIce() {
        return powerPlayTimeOnIce;
    }

    public String getEvenTimeOnIce() {
        return evenTimeOnIce;
    }

    public String getPenaltyMinutes() {
        return penaltyMinutes;
    }

    public float getFaceOffPct() {
        return faceOffPct;
    }

    public float getShotPct() {
        return shotPct;
    }

    public float getGameWinningGoals() {
        return gameWinningGoals;
    }

    public float getOverTimeGoals() {
        return overTimeGoals;
    }

    public float getShortHandedGoals() {
        return shortHandedGoals;
    }

    public float getShortHandedPoints() {
        return shortHandedPoints;
    }

    public String getShortHandedTimeOnIce() {
        return shortHandedTimeOnIce;
    }

    public float getBlocked() {
        return blocked;
    }

    public float getPlusMinus() {
        return plusMinus;
    }

    public float getPoints() {
        return points;
    }

    public float getShifts() {
        return shifts;
    }

    public String getTimeOnIcePerGame() {
        return timeOnIcePerGame;
    }

    public String getEvenTimeOnIcePerGame() {
        return evenTimeOnIcePerGame;
    }

    public String getShortHandedTimeOnIcePerGame() {
        return shortHandedTimeOnIcePerGame;
    }

    public String getPowerPlayTimeOnIcePerGame() {
        return powerPlayTimeOnIcePerGame;
    }

    // Setter Methods

    public void setTimeOnIce(String timeOnIce) {
        this.timeOnIce = timeOnIce;
    }

    public void setAssists(float assists) {
        this.assists = assists;
    }

    public void setGoals(float goals) {
        this.goals = goals;
    }

    public void setPim(float pim) {
        this.pim = pim;
    }

    public void setShots(float shots) {
        this.shots = shots;
    }

    public void setGames(float games) {
        this.games = games;
    }

    public void setHits(float hits) {
        this.hits = hits;
    }

    public void setPowerPlayGoals(float powerPlayGoals) {
        this.powerPlayGoals = powerPlayGoals;
    }

    public void setPowerPlayPoints(float powerPlayPoints) {
        this.powerPlayPoints = powerPlayPoints;
    }

    public void setPowerPlayTimeOnIce(String powerPlayTimeOnIce) {
        this.powerPlayTimeOnIce = powerPlayTimeOnIce;
    }

    public void setEvenTimeOnIce(String evenTimeOnIce) {
        this.evenTimeOnIce = evenTimeOnIce;
    }

    public void setPenaltyMinutes(String penaltyMinutes) {
        this.penaltyMinutes = penaltyMinutes;
    }

    public void setFaceOffPct(float faceOffPct) {
        this.faceOffPct = faceOffPct;
    }

    public void setShotPct(float shotPct) {
        this.shotPct = shotPct;
    }

    public void setGameWinningGoals(float gameWinningGoals) {
        this.gameWinningGoals = gameWinningGoals;
    }

    public void setOverTimeGoals(float overTimeGoals) {
        this.overTimeGoals = overTimeGoals;
    }

    public void setShortHandedGoals(float shortHandedGoals) {
        this.shortHandedGoals = shortHandedGoals;
    }

    public void setShortHandedPoints(float shortHandedPoints) {
        this.shortHandedPoints = shortHandedPoints;
    }

    public void setShortHandedTimeOnIce(String shortHandedTimeOnIce) {
        this.shortHandedTimeOnIce = shortHandedTimeOnIce;
    }

    public void setBlocked(float blocked) {
        this.blocked = blocked;
    }

    public void setPlusMinus(float plusMinus) {
        this.plusMinus = plusMinus;
    }

    public void setPoints(float points) {
        this.points = points;
    }

    public void setShifts(float shifts) {
        this.shifts = shifts;
    }

    public void setTimeOnIcePerGame(String timeOnIcePerGame) {
        this.timeOnIcePerGame = timeOnIcePerGame;
    }

    public void setEvenTimeOnIcePerGame(String evenTimeOnIcePerGame) {
        this.evenTimeOnIcePerGame = evenTimeOnIcePerGame;
    }

    public void setShortHandedTimeOnIcePerGame(String shortHandedTimeOnIcePerGame) {
        this.shortHandedTimeOnIcePerGame = shortHandedTimeOnIcePerGame;
    }

    public void setPowerPlayTimeOnIcePerGame(String powerPlayTimeOnIcePerGame) {
        this.powerPlayTimeOnIcePerGame = powerPlayTimeOnIcePerGame;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM