使用Gson解析Json字符串含有動(dòng)態(tài)key(Dynamic Key)時(shí),如何定義Gson所映射的對(duì)象

在使用Gson解析Json字符串的時(shí)候彰亥,遇到動(dòng)態(tài)key咧七,如何定義Gson所映射對(duì)象?
以下所述方法同樣適用于Jackson解析
請(qǐng)看如下Json字符串:

{
  "code": 200,
  "msg": "成功",
  "data": [
    {
      "id": "section分割模塊id",
      "tid": "t_competition_schedule_section_1",
      "data": {
        "source": "json",
        "dlist": [
          {
            "122246": {
              "id": 4499,
              "hostid": 1416,
              "hostname": "曼聯(lián)",
              "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輪 曼聯(lián)vs阿森納",
              "sectionid": "122246",
              "isversus": true,
              "hasalarm": false,
              "isrecommend": false,
              "type": 1
            }
          }
        ]
      }
    }
  ]
}

122246 這個(gè)key是動(dòng)態(tài)的

這個(gè)時(shí)候任斋,我們可以使用LinkedHashMap<String, Object> dlist继阻;來映射122246所對(duì)應(yīng)的對(duì)象。

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

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

        }
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末废酷,一起剝皮案震驚了整個(gè)濱河市瘟檩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌澈蟆,老刑警劉巖墨辛,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異趴俘,居然都是意外死亡睹簇,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門哮幢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來带膀,“玉大人,你說我怎么就攤上這事橙垢《膺叮” “怎么了?”我有些...
    開封第一講書人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵柜某,是天一觀的道長(zhǎng)嗽元。 經(jīng)常有香客問我,道長(zhǎng)喂击,這世上最難降的妖魔是什么剂癌? 我笑而不...
    開封第一講書人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮翰绊,結(jié)果婚禮上佩谷,老公的妹妹穿的比我還像新娘旁壮。我一直安慰自己,他們只是感情好谐檀,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開白布抡谐。 她就那樣靜靜地躺著,像睡著了一般桐猬。 火紅的嫁衣襯著肌膚如雪麦撵。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,604評(píng)論 1 305
  • 那天溃肪,我揣著相機(jī)與錄音免胃,去河邊找鬼。 笑死惫撰,一個(gè)胖子當(dāng)著我的面吹牛羔沙,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播润绎,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼撬碟,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼诞挨!你這毒婦竟也來了莉撇?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤惶傻,失蹤者是張志新(化名)和其女友劉穎棍郎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體银室,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡涂佃,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蜈敢。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辜荠。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖抓狭,靈堂內(nèi)的尸體忽然破棺而出伯病,到底是詐尸還是另有隱情,我是刑警寧澤否过,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布午笛,位于F島的核電站,受9級(jí)特大地震影響苗桂,放射性物質(zhì)發(fā)生泄漏药磺。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一煤伟、第九天 我趴在偏房一處隱蔽的房頂上張望癌佩。 院中可真熱鬧木缝,春花似錦、人聲如沸围辙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽酌畜。三九已至怎囚,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間桥胞,已是汗流浹背恳守。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留贩虾,地道東北人催烘。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像缎罢,于是被迫代替她去往敵國(guó)和親伊群。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 1.概述2.Gson的目標(biāo)3.Gson的性能和擴(kuò)展性4.Gson的使用者5.如何使用Gson 通過Maven來使用...
    人失格閱讀 14,236評(píng)論 2 18
  • 轉(zhuǎn)載:http://www.reibang.com/p/b87fee2f7a23 前言 今天丸卷,我們來介紹現(xiàn)今主流的...
    朝花夕拾不起來閱讀 3,208評(píng)論 2 27
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)询刹,斷路器谜嫉,智...
    卡卡羅2017閱讀 134,657評(píng)論 18 139
  • 此刻我坐在新搬的地方,雖說是宿舍也是一個(gè)人分享一室一廳凹联,有暖氣有陽臺(tái)沐兰,陽光可以透過窗戶照進(jìn)來,暖暖的蔽挠,特別是...
    止戈小喬閱讀 161評(píng)論 0 0
  • 有些朋友住闯,一直不見煌珊,偶爾見一面钾麸,仍然有的聊,因?yàn)楣亲永锏膬r(jià)值觀是一致的踱葛。有些則象交叉線偶惠,在某一個(gè)點(diǎn)重合春寿,之后漸行漸遠(yuǎn)。
    呶呶的簡(jiǎn)書閱讀 190評(píng)論 0 0