使用Gson解析Json字符串含有动态key(Dynamic K

2016-12-23  本文已影响729人  littlezan

在使用Gson解析Json字符串的时候,遇到动态key,如何定义Gson所映射对象?
以下所述方法同样适用于Jackson解析
请看如下Json字符串:

{
  "code": 200,
  "msg": "成功",
  "data": [
    {
      "id": "section分割模块id",
      "tid": "t_competition_schedule_section_1",
      "data": {
        "source": "json",
        "dlist": [
          {
            "122246": {
              "id": 4499,
              "hostid": 1416,
              "hostname": "曼联",
              "hosticon": "http://grocery.pptv.com/lpic/fc3/0f9/893/78688b9029d005028bcfe7d91ce94770.png",
              "guestid": 1380,
              "guestname": "阿森纳",
              "guesticon": "http://grocery.pptv.com/lpic/973/360/548/28af893a78b5d4dc3e0a7af90b9766b4.png",
              "hostscore": 1,
              "guestscore": 1,
              "starttime": 1479558600000,
              "endtime": 1479565800000,
              "round": "第12轮",
              "competitionid": 44,
              "competitionname": "英超",
              "competitionicon": "http://grocery.pptv.com/lpic/beb/ddd/6e9/acbd2ce6346bae749d76fe1ec0adf4f3.png",
              "schedulemark": "英超 第12轮",
              "reservetitle": "英超 第12轮 曼联vs阿森纳",
              "sectionid": "122246",
              "isversus": true,
              "hasalarm": false,
              "isrecommend": false,
              "type": 1
            }
          }
        ]
      }
    }
  ]
}

122246 这个key是动态的

这个时候,我们可以使用LinkedHashMap<String, Object> dlist;来映射122246所对应的对象。

然后使用SimpleBean bean = new Gson().fromJson(jsonResult, SimpleBean.class)
完整的对象如下(供参考):

public class SimpleBean {


    @SerializedName("code")
    private int code;
    @SerializedName("msg")
    private String msg;
    @SerializedName("data")
    private List<DataBeanX> data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<DataBeanX> getData() {
        return data;
    }

    public void setData(List<DataBeanX> data) {
        this.data = data;
    }

    public static class DataBeanX {

        @SerializedName("id")
        private String id;
        @SerializedName("tid")
        private String tid;
        @SerializedName("data")
        private DataBean data;


        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTid() {
            return tid;
        }

        public void setTid(String tid) {
            this.tid = tid;
        }

        public DataBean getData() {
            return data;
        }

        public void setData(DataBean data) {
            this.data = data;
        }

        public static class DataBean {

            @SerializedName("source")
            private String source;
            @SerializedName("dlist")
            private List<LinkedHashMap<String, Section>> dlist;


            public String getSource() {
                return source;
            }

            public void setSource(String source) {
                this.source = source;
            }

            public List<LinkedHashMap<String, Section>> getDlist() {
                return dlist;
            }

            public void setDlist(List<LinkedHashMap<String, Section>> dlist) {
                this.dlist = dlist;
            }

            public static class Section {


                @SerializedName("id")
                private int id;
                @SerializedName("hostid")
                private int hostid;
                @SerializedName("hostname")
                private String hostname;
                @SerializedName("hosticon")
                private String hosticon;
                @SerializedName("guestid")
                private int guestid;
                @SerializedName("guestname")
                private String guestname;
                @SerializedName("guesticon")
                private String guesticon;
                @SerializedName("hostscore")
                private int hostscore;
                @SerializedName("guestscore")
                private int guestscore;
                @SerializedName("starttime")
                private long starttime;
                @SerializedName("endtime")
                private long endtime;
                @SerializedName("round")
                private String round;
                @SerializedName("competitionid")
                private int competitionid;
                @SerializedName("competitionname")
                private String competitionname;
                @SerializedName("competitionicon")
                private String competitionicon;
                @SerializedName("schedulemark")
                private String schedulemark;
                @SerializedName("reservetitle")
                private String reservetitle;
                @SerializedName("sectionid")
                private String sectionid;
                @SerializedName("isversus")
                private boolean isversus;
                @SerializedName("hasalarm")
                private boolean hasalarm;
                @SerializedName("isrecommend")
                private boolean isrecommend;
                @SerializedName("type")
                private int type;

                public static Section objectFromData(String str) {

                    return new Gson().fromJson(str, Section.class);
                }

                public int getId() {
                    return id;
                }

                public void setId(int id) {
                    this.id = id;
                }

                public int getHostid() {
                    return hostid;
                }

                public void setHostid(int hostid) {
                    this.hostid = hostid;
                }

                public String getHostname() {
                    return hostname;
                }

                public void setHostname(String hostname) {
                    this.hostname = hostname;
                }

                public String getHosticon() {
                    return hosticon;
                }

                public void setHosticon(String hosticon) {
                    this.hosticon = hosticon;
                }

                public int getGuestid() {
                    return guestid;
                }

                public void setGuestid(int guestid) {
                    this.guestid = guestid;
                }

                public String getGuestname() {
                    return guestname;
                }

                public void setGuestname(String guestname) {
                    this.guestname = guestname;
                }

                public String getGuesticon() {
                    return guesticon;
                }

                public void setGuesticon(String guesticon) {
                    this.guesticon = guesticon;
                }

                public int getHostscore() {
                    return hostscore;
                }

                public void setHostscore(int hostscore) {
                    this.hostscore = hostscore;
                }

                public int getGuestscore() {
                    return guestscore;
                }

                public void setGuestscore(int guestscore) {
                    this.guestscore = guestscore;
                }

                public long getStarttime() {
                    return starttime;
                }

                public void setStarttime(long starttime) {
                    this.starttime = starttime;
                }

                public long getEndtime() {
                    return endtime;
                }

                public void setEndtime(long endtime) {
                    this.endtime = endtime;
                }

                public String getRound() {
                    return round;
                }

                public void setRound(String round) {
                    this.round = round;
                }

                public int getCompetitionid() {
                    return competitionid;
                }

                public void setCompetitionid(int competitionid) {
                    this.competitionid = competitionid;
                }

                public String getCompetitionname() {
                    return competitionname;
                }

                public void setCompetitionname(String competitionname) {
                    this.competitionname = competitionname;
                }

                public String getCompetitionicon() {
                    return competitionicon;
                }

                public void setCompetitionicon(String competitionicon) {
                    this.competitionicon = competitionicon;
                }

                public String getSchedulemark() {
                    return schedulemark;
                }

                public void setSchedulemark(String schedulemark) {
                    this.schedulemark = schedulemark;
                }

                public String getReservetitle() {
                    return reservetitle;
                }

                public void setReservetitle(String reservetitle) {
                    this.reservetitle = reservetitle;
                }

                public String getSectionid() {
                    return sectionid;
                }

                public void setSectionid(String sectionid) {
                    this.sectionid = sectionid;
                }

                public boolean isIsversus() {
                    return isversus;
                }

                public void setIsversus(boolean isversus) {
                    this.isversus = isversus;
                }

                public boolean isHasalarm() {
                    return hasalarm;
                }

                public void setHasalarm(boolean hasalarm) {
                    this.hasalarm = hasalarm;
                }

                public boolean isIsrecommend() {
                    return isrecommend;
                }

                public void setIsrecommend(boolean isrecommend) {
                    this.isrecommend = isrecommend;
                }

                public int getType() {
                    return type;
                }

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

        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读