代码之家  ›  专栏  ›  技术社区  ›  BekaKK

在EnumClass中用Gson解析Json

  •  0
  • BekaKK  · 技术社区  · 6 年前

     I have like this json

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

    我是这样用Gson的

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

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

    如何用枚举类重写代码? 谢谢

    0 回复  |  直到 5 年前