0

 I have like this json

I have like this json.I'm using Gson to parse it and convert it in my custom class object.Here is a my java classes

public class ResponseModel {
    private int resultCode;
    private Match match;

    public Match getMatch() {
        return match;
    }
    public int getResultCode() {
        return resultCode;

    }
}

public class Match {

    private Team team1;
    private Team team2;

    private double matchTime;

    public Team getTeam1() {
        return team1;
    }

    public Team getTeam2() {
        return team2;
    }

  private Long matchDate;
    private String stadiumAdress;

    public double getMatchTime() {
        return matchTime;
    }

    public Long getMatchDate() {
        return matchDate;
    }

    public String getStadiumAdress() {
        return stadiumAdress;
    }
}

public class Team {
    private String teamName;
    private String teamImage;

    public String getTeamName() {
        return teamName;
    }

    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }

    public String getTeamImage() {
        return teamImage;
    }

    public void setTeamImage(String teamImage) {
        this.teamImage = teamImage;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getBallPosition() {
        return ballPosition;
    }

    public void setBallPosition(int ballPosition) {
        this.ballPosition = ballPosition;
    }

    private int score;
    private int ballPosition;
}

I'm using Gson like this


ResponseModel responseModel = GsonUtil.fromJson(response.toString(), ResponseModel.class);

public class GsonUtil {

    public static <T> T fromJson(String json, Class<T> c) {
        return new Gson().fromJson(json, c);
    }

    public static String toJson(Object c) {
        return new Gson().toJson(c);
    }

}

Everything working perfect,I can convert my json to custom class.But I want to use enum class with team1 and team2. My goal is to convert like this enum class

MatchTeamType:
TEAM1 (1);
TEAM2 (2);

How I can rewrite my code with enum class? Thanks

Jin Lee
  • 3,194
  • 12
  • 46
  • 86
BekaKK
  • 2,173
  • 6
  • 42
  • 80
  • Depending on the version of Gson you are using, you could use the`SerializedName` annotation. See this [post](https://stackoverflow.com/questions/16740078/serialize-and-deserialize-enum-with-gson). – TheDude Sep 12 '18 at 18:04
  • 1
    What exactly is `MatchTeamType`? Is it the team name or something inferred from the Json only known to your domain? – Fred Sep 13 '18 at 10:17
  • it’s not clear to me what you’re trying to do – Elkhan Ibrahimov Sep 13 '18 at 13:54

0 Answers0