Gson反序列化具有相同的类名

时间:2017-05-16 07:50:24

标签: java android json gson

我正在尝试使用Gson将JSON反序列化为POJO,但它不起作用。

这是我的JSON:

"eu":{
        "stats":{
            "competitive":{
                "average_stats":{
                    "solo_kills_avg":3.28,
                    "objective_kills_avg":9.53,
                    "objective_time_avg":0.01972222222222222,
                    "time_spent_on_fire_avg":0.03027777777777778,
                    "final_blows_avg":13.74,
                    "eliminations_avg":24.53,
                    "deaths_avg":10.2,
                    "damage_done_avg":13758.0,
                    "melee_final_blows_avg":0.3,
                    "healing_done_avg":1985.0
                },
                "overall_stats":{
                    "win_rate":48.0,
                    "avatar":"https://blzgdapipro-a.akamaihd.net/game/unlocks/0x02500000000008E9.png",
                    "losses":91,
                    "prestige":2,
                    "wins":84,
                    "rank_image":"https://blzgdapipro-a.akamaihd.net/game/playerlevelrewards/0x0250000000000948_Border.png",
                    "ties":7,
                    "games":182,
                    "tier":"gold",
                    "level":74,
                    "comprank":2183
                }
            },
            "quickplay":{
                "average_stats":{
                    "solo_kills_avg":2.28,
                    "objective_kills_avg":4.22,
                    "objective_time_avg":0.01,
                    "time_spent_on_fire_avg":0.013055555555555555,
                    "final_blows_avg":6.65,
                    "eliminations_avg":12.36,
                    "deaths_avg":6.8,
                    "damage_done_avg":5132.0,
                    "melee_final_blows_avg":0.15,
                    "healing_done_avg":958.0
                },
                "overall_stats":{
                    "wins":551,
                    "avatar":"https://blzgdapipro-a.akamaihd.net/game/unlocks/0x02500000000008E9.png",
                    "losses":511,
                    "prestige":2,
                    "rank_image":"https://blzgdapipro-a.akamaihd.net/game/playerlevelrewards/0x0250000000000948_Border.png",
                    "games":1062,
                    "tier":"gold",
                    "ties":7,
                    "level":74,
                    "win_rate":51.88,
                    "comprank":2183
                }
            }
        }
    }

正如您所看到的那样,average_statsoverall_stats同时适用于快速播放和竞争,但我只能从竞争中获取average_statsoverall_stats的数据,而不是来自快速播放。

我将此site用于生成类

Eu class:

public class Eu implements Parcelable
{
    private Stats stats;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    public final static Creator<Eu> CREATOR = new Creator<Eu>() {

        @SuppressWarnings({
                "unchecked"
        })
        public Eu createFromParcel(Parcel in) {
            Eu instance = new Eu();
            instance.stats = ((Stats) in.readValue((Stats.class.getClassLoader())));
            instance.additionalProperties = ((Map<String, Object> ) in.readValue((Map.class.getClassLoader())));
            return instance;
        }

        public Eu[] newArray(int size) {
            return (new Eu[size]);
        }

    }
            ;

    public Stats getStats() {
        return stats;
    }

    public void setStats(Stats stats) {
        this.stats = stats;
    }

    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(stats);
        dest.writeValue(additionalProperties);
    }

    public int describeContents() {
        return 0;
    }

}

统计类:

public class Stats implements Parcelable
{

    private Competitive competitive;
    private Quickplay quickplay;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    public final static Creator<Stats> CREATOR = new Creator<Stats>() {


        @SuppressWarnings({
                "unchecked"
        })
        public Stats createFromParcel(Parcel in) {
            Stats instance = new Stats();
            instance.competitive = ((Competitive) in.readValue((Competitive.class.getClassLoader())));
            instance.quickplay = ((Quickplay) in.readValue((Quickplay.class.getClassLoader())));
            instance.additionalProperties = ((Map<String, Object> ) in.readValue((Map.class.getClassLoader())));
            return instance;
        }

        public Stats[] newArray(int size) {
            return (new Stats[size]);
        }

    }
            ;

    public Competitive getCompetitive() {
        return competitive;
    }

    public void setCompetitive(Competitive competitive) {
        this.competitive = competitive;
    }

    public Quickplay getQuickplay() {
        return quickplay;
    }

    public void setQuickplay(Quickplay quickplay) {
        this.quickplay = quickplay;
    }

    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(competitive);
        dest.writeValue(quickplay);
        dest.writeValue(additionalProperties);
    }

    public int describeContents() {
        return 0;
    }

}

竞争性课程:

public class Competitive implements Parcelable
{

    private Overall_stats overall_stats;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    public final static Creator<Competitive> CREATOR = new Creator<Competitive>() {


        @SuppressWarnings({
                "unchecked"
        })
        public Competitive createFromParcel(Parcel in) {
            Competitive instance = new Competitive();
            instance.overall_stats = ((Overall_stats) in.readValue((Overall_stats.class.getClassLoader())));
            instance.additionalProperties = ((Map<String, Object> ) in.readValue((Map.class.getClassLoader())));
            return instance;
        }

        public Competitive[] newArray(int size) {
            return (new Competitive[size]);
        }

    }
            ;

    public Overall_stats getOverall_stats() {
        return overall_stats;
    }

    public void setOverall_stats(Overall_stats overall_stats) {
        this.overall_stats = overall_stats;
    }

    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(overall_stats);
        dest.writeValue(additionalProperties);
    }

    public int describeContents() {
        return 0;
    }

}

快速播放课程:

public class Quickplay implements Parcelable
{

    private Overall_stats Overall_stats;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    public final static Creator<Quickplay> CREATOR = new Creator<Quickplay>() {


        @SuppressWarnings({
                "unchecked"
        })
        public Quickplay createFromParcel(Parcel in) {
            Quickplay instance = new Quickplay();
            instance.Overall_stats = ((Overall_stats) in.readValue((Overall_stats.class.getClassLoader())));
            instance.additionalProperties = ((Map<String, Object> ) in.readValue((Map.class.getClassLoader())));
            return instance;
        }

        public Quickplay[] newArray(int size) {
            return (new Quickplay[size]);
        }

    }
            ;

    public Overall_stats getOverall_stats() {
        return Overall_stats;
    }

    public void setOverall_stats(Overall_stats Overall_stats) {
        this.Overall_stats = Overall_stats;
    }

    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(Overall_stats);
        dest.writeValue(additionalProperties);
    }

    public int describeContents() {
        return 0;
    }

}

Overall_stats课程:

public class Overall_stats implements Parcelable
{

    private float win_rate;
    private String avatar;
    private int losses;
    private int prestige;
    private int wins;
    private String rank_image;
    private int ties;
    private int games;
    private String tier;
    private int level;
    private int comprank;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    public final static Creator<Overall_stats> CREATOR = new Creator<Overall_stats>() {


        @SuppressWarnings({
                "unchecked"
        })
        public Overall_stats createFromParcel(Parcel in) {
            Overall_stats instance = new Overall_stats();
            instance.win_rate = ((float) in.readValue((float.class.getClassLoader())));
            instance.avatar = ((String) in.readValue((String.class.getClassLoader())));
            instance.losses = ((int) in.readValue((int.class.getClassLoader())));
            instance.prestige = ((int) in.readValue((int.class.getClassLoader())));
            instance.wins = ((int) in.readValue((int.class.getClassLoader())));
            instance.rank_image = ((String) in.readValue((String.class.getClassLoader())));
            instance.ties = ((int) in.readValue((int.class.getClassLoader())));
            instance.games = ((int) in.readValue((int.class.getClassLoader())));
            instance.tier = ((String) in.readValue((String.class.getClassLoader())));
            instance.level = ((int) in.readValue((int.class.getClassLoader())));
            instance.comprank = ((int) in.readValue((int.class.getClassLoader())));
            instance.additionalProperties = ((Map<String, Object> ) in.readValue((Map.class.getClassLoader())));
            return instance;
        }

        public Overall_stats[] newArray(int size) {
            return (new Overall_stats[size]);
        }

    }
            ;

    public float getWin_rate() {
        return win_rate;
    }

    public void setWin_rate(float win_rate) {
        this.win_rate = win_rate;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public int getLosses() {
        return losses;
    }

    public void setLosses(int losses) {
        this.losses = losses;
    }

    public int getPrestige() {
        return prestige;
    }

    public void setPrestige(int prestige) {
        this.prestige = prestige;
    }

    public int getWins() {
        return wins;
    }

    public void setWins(int wins) {
        this.wins = wins;
    }

    public String getRank_image() {
        return rank_image;
    }

    public void setRank_image(String rank_image) {
        this.rank_image = rank_image;
    }

    public int getTies() {
        return ties;
    }

    public void setTies(int ties) {
        this.ties = ties;
    }

    public int getGames() {
        return games;
    }

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

    public String getTier() {
        return tier;
    }

    public void setTier(String tier) {
        this.tier = tier;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getComprank() {
        return comprank;
    }

    public void setComprank(int comprank) {
        this.comprank = comprank;
    }

    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(win_rate);
        dest.writeValue(avatar);
        dest.writeValue(losses);
        dest.writeValue(prestige);
        dest.writeValue(wins);
        dest.writeValue(rank_image);
        dest.writeValue(ties);
        dest.writeValue(games);
        dest.writeValue(tier);
        dest.writeValue(level);
        dest.writeValue(comprank);
        dest.writeValue(additionalProperties);
    }

    public int describeContents() {
        return 0;
    }

}

这有效:

playerStats.getEu().getStats().getCompetitive().getOverall_stats().getGames();

这不起作用:

playerStats.getEu().getStats().getQuickplay().getOverall_stats().getGames();

Gson不喜欢不同对象的同名类吗?

2 个答案:

答案 0 :(得分:0)

快速播放中的字段名称有错误

应该是overall_stats而不是Overall_stats,

但实际上你应该使用由

注释的java camelCase参数名称

这是示例

 @SerializedName("overall_stats")
private OverallStats overallStats

答案 1 :(得分:0)

试试这个json:

{
  "us": {
    "stats": {
      "competitive": {
        "average_stats": {
          "solo_kills_avg": 3.28,
          "objective_kills_avg": 9.53,
          "objective_time_avg": 0.01972222222222222,
          "time_spent_on_fire_avg": 0.03027777777777778,
          "final_blows_avg": 13.74,
          "eliminations_avg": 24.53,
          "deaths_avg": 10.2,
          "damage_done_avg": 13758.0,
          "melee_final_blows_avg": 0.3,
          "healing_done_avg": 1985.0
        },
        "overall_stats": {
          "win_rate": 48.0,
          "avatar": "https://blzgdapipro-a.akamaihd.net/game/unlocks/0x02500000000008E9.png",
          "losses": 91,
          "prestige": 2,
          "wins": 84,
          "rank_image": "https://blzgdapipro-a.akamaihd.net/game/playerlevelrewards/0x0250000000000948_Border.png",
          "ties": 7,
          "games": 182,
          "tier": "gold",
          "level": 74,
          "comprank": 2183
        }
      },
      "quickplay": {
        "average_stats": {
          "solo_kills_avg": 2.28,
          "objective_kills_avg": 4.22,
          "objective_time_avg": 0.01,
          "time_spent_on_fire_avg": 0.013055555555555555,
          "final_blows_avg": 6.65,
          "eliminations_avg": 12.36,
          "deaths_avg": 6.8,
          "damage_done_avg": 5132.0,
          "melee_final_blows_avg": 0.15,
          "healing_done_avg": 958.0
        },
        "overall_stats": {
          "wins": 551,
          "avatar": "https://blzgdapipro-a.akamaihd.net/game/unlocks/0x02500000000008E9.png",
          "losses": 511,
          "prestige": 2,
          "rank_image": "https://blzgdapipro-a.akamaihd.net/game/playerlevelrewards/0x0250000000000948_Border.png",
          "games": 1062,
          "tier": "gold",
          "ties": 7,
          "level": 74,
          "win_rate": 51.88,
          "comprank": 2183
        }
      }
    }
  }
}