美文网首页Android知识
Android中Gson、FastJson解析动态的Key

Android中Gson、FastJson解析动态的Key

作者: 烂吹笙 | 来源:发表于2018-02-06 22:51 被阅读0次

在项目开发中一般只会出现JsonObject、JsonArray。他们的key一般都是恒定不变,如果变化,那么后台程序员可能会变前端程序员打死,实际开发中,我们应该避免这些问题。但是如果遇到这种问题,我们该如果应对呢,在Android中我们使用解析的第三方库一般都是GsonFastJson。下面介绍下遇到动态变化的key我们该如果处理。

首先看数据

{"ret":0,"data":{"categories":[{"id":7,"name":"推荐"},{"id":1,"name":"中超"},{"id":2,"name":"英超"},{"id":3,"name":"西甲"},{"id":4,"name":"德甲"},{"id":5,"name":"意甲"},{"id":6,"name":"法甲"},{"id":9,"name":"国家队"},{"id":8,"name":"更多"}],"categoryGroupsMap":{"1":[{"id":7,"name":"广州恒大淘宝","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/2_1464603624_1464603647760.png","cover":"http://cdn.qiudd.net/4_1488358920_1488358927143.jpg","groupType":1,"teamId":126},{"id":8,"name":"江苏苏宁易购","icon":"http://cdn.qiudd.net/4_1488857100_1488857325614.png","cover":"http://cdn.qiudd.net/4_1488857100_1488857341287.jpg","groupType":1,"teamId":122},{"id":9,"name":"上海上港","icon":"http://cdn.qiudd.net/4_1488857027_1488857069208.jpg","cover":"http://cdn.qiudd.net/4_1488857344_1488857355733.jpg","groupType":1,"teamId":127},{"id":10,"name":"上海绿地申花","icon":"http://cdn.qiudd.net/4_1488857085_1488857095450.jpg","cover":"http://cdn.qiudd.net/4_1488857359_1488857368676.jpg","groupType":1,"teamId":119},{"id":11,"name":"河北华夏幸福","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/2_1464603698_1464603706613.png","cover":"http://cdn.qiudd.net/4_1488857372_1488857384008.jpg","groupType":1,"teamId":130},{"id":12,"name":"广州富力","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/425_54def14ebc5c4086d964a117ccf1df73.png","cover":"http://cdn.qiudd.net/4_1488857388_1488857397145.jpg","groupType":1,"teamId":115},{"id":14,"name":"河南建业","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/442_70d48b0eb5cbf345e29a24bc2c444883.png","cover":"http://cdn.qiudd.net/4_1488857413_1488857422309.jpg","groupType":1,"teamId":123},{"id":15,"name":"北京中赫国安","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/429_fa5e518128f1e30afc980b598b252795.png","cover":"http://cdn.qiudd.net/4_1488358942_1488358949257.jpg","groupType":1,"teamId":116},{"id":17,"name":"重庆当代力帆","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/432_afe297b33971d0f54ce2497498ca6e51.png","cover":"http://cdn.qiudd.net/4_1488857446_1488857454890.jpg","groupType":1,"teamId":118},{"id":18,"name":"天津亿利","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/2_1464603860_1464603868436.png","cover":"http://cdn.qiudd.net/4_1488857460_1488857468047.jpg","groupType":1,"teamId":117},{"id":20,"name":"山东鲁能泰山","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/2_1464603746_1464603754372.png","cover":"http://cdn.qiudd.net/4_1488359080_1488359085086.jpg","groupType":1,"teamId":120},{"id":22,"name":"长春亚泰","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/437_b8af22171514a04fdb5ce317d63026cb.png","cover":"http://cdn.qiudd.net/83_1475563545_1475563555855.jpg","groupType":1,"teamId":121},{"id":71,"name":"贵州恒丰智诚","icon":"http://cdn.qiudd.net/87_1487140072_1487140173563.png","cover":"","groupType":1,"teamId":129},{"id":72,"name":"天津权健","icon":"http://cdn.qiudd.net/10_1486348064_1486348069080.jpg","cover":"","groupType":1,"teamId":128},{"id":97,"name":"中超联赛","icon":"http://cdn.qiudd.net/52_1496634099_1496634104297.png","cover":"http://cdn.qiudd.net/52_1496634099_1496634111689.jpg","groupType":1,"teamId":0},{"id":103,"name":"大连一方","icon":"http://zq.win007.com/Image/team/images/20170609110409.png","cover":"","groupType":1,"teamId":2784},{"id":104,"name":"北京人和","icon":"http://zq.win007.com/Image/team/images/20170408155503.jpg","cover":"","groupType":1,"teamId":2781}],"2":[{"id":23,"name":"曼联","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/662_c83f70cde952353e3bd25daf96b0972d.png","cover":"http://cdn.qiudd.net/4_1488358706_1488358716679.jpg","groupType":1,"teamId":143},{"id":24,"name":"曼城","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/45_1468988657_1468988669263.png","cover":"http://cdn.qiudd.net/4_1488358722_1488358726838.jpg","groupType":1,"teamId":145},{"id":25,"name":"切尔西","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/661_615274b59dd4e7749cdce02d0e5ea639.png","cover":"http://cdn.qiudd.net/4_1488358732_1488358736635.jpg","groupType":1,"teamId":142},{"id":26,"name":"阿森纳","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/660_5d4beb410e8b3da7316dbf8cf333b455.png","cover":"http://cdn.qiudd.net/4_1488358639_1488358662297.jpg","groupType":1,"teamId":141},{"id":27,"name":"利物浦","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/663_be794e2758f3087f637d2122fb3d8c3e.png","cover":"http://cdn.qiudd.net/4_1488358744_1488358750667.jpg","groupType":1,"teamId":399},{"id":28,"name":"托特纳姆热刺","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/675_589fc59f335b2b6578a2a5d96320eac3.png","cover":"http://cdn.qiudd.net/4_1488858499_1488858522945.jpg","groupType":1,"teamId":144},{"id":29,"name":"莱斯特城","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/682_f99552dc74fcdae44e39352aef651e43.png","cover":"http://cdn.qiudd.net/4_1488858528_1488858537397.jpg","groupType":1,"teamId":575},{"id":80,"name":"英超部落","icon":"http://cdn.qiudd.net/52_1487489497_1487489474294.png","cover":"http://cdn.qiudd.net/52_1487489497_1487489480877.jpg","groupType":1,"teamId":0}],"3":[{"id":30,"name":"皇家马德里","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/2016_f7233dcda8045400203254694da6dffb.png","cover":"http://cdn.qiudd.net/4_1488358794_1488358800683.jpg","groupType":1,"teamId":151},{"id":31,"name":"巴塞罗那","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/2017_bab20eb5fb3bd3073beca64ce07c1496.png","cover":"http://cdn.qiudd.net/4_1488358807_1488358815614.jpg","groupType":1,"teamId":152},{"id":32,"name":"马德里竞技","icon":"http://cdn.qiudd.net/52_1498649945_1498649987674.png","cover":"http://cdn.qiudd.net/52_1498647244_1498648262391.jpg","groupType":1,"teamId":400},{"id":77,"name":"西甲部落","icon":"http://cdn.qiudd.net/52_1487489356_1487489329863.png","cover":"http://cdn.qiudd.net/52_1487488865_1487488862418.jpg","groupType":1,"teamId":0}],"4":[{"id":36,"name":"拜仁慕尼黑","icon":"http://cdn.qiudd.net/52_1498650007_1498650014238.png","cover":"http://cdn.qiudd.net/52_1498648286_1498648394178.jpg","groupType":1,"teamId":2},{"id":37,"name":"多特蒙德","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/964_9c44ed617fda0abb62349acdc09d505b.png","cover":"http://cdn.qiudd.net/4_1488358872_1488358877046.jpg","groupType":1,"teamId":4},{"id":38,"name":"沙尔克04","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/966_fd0b3aa84ed3da3cd2001b4000342d0f.png","cover":"http://cdn.qiudd.net/4_1488954987_1488954994497.jpg","groupType":1,"teamId":5},{"id":79,"name":"德甲部落","icon":"http://cdn.qiudd.net/52_1487489426_1487489402483.png","cover":"http://cdn.qiudd.net/52_1487490151_1487490128062.jpg","groupType":1,"teamId":0}],"5":[{"id":40,"name":"AC米兰","icon":"http://cdn.qiudd.net/52_1478660777_1478660756963.png","cover":"http://cdn.qiudd.net/4_1488359221_1488359226588.jpg","groupType":1,"teamId":147},{"id":41,"name":"国际米兰","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/45_1469167738_1469167745584.png","cover":"http://cdn.qiudd.net/4_1488359238_1488359279429.jpg","groupType":1,"teamId":150},{"id":42,"name":"尤文图斯","icon":"http://cdn.qiudd.net/52_1487818567_1487818660687.png","cover":"http://cdn.qiudd.net/4_1488955157_1488955167036.jpg","groupType":1,"teamId":149},{"id":43,"name":"罗马","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/1241_2784146a49b1d639b840262cbea9651c.png","cover":"http://cdn.qiudd.net/4_1488359375_1488359380698.jpg","groupType":1,"teamId":148},{"id":44,"name":"那不勒斯","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/1270_910ee660d284fd42d6d90bea705731f6.png","cover":"http://cdn.qiudd.net/4_1488955042_1488955048974.jpg","groupType":1,"teamId":1778},{"id":46,"name":"拉齐奥","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/1245_f3d2c62743abc31f231cd2c69500ab87.png","cover":"http://cdn.qiudd.net/4_1488955071_1488955078465.jpg","groupType":1,"teamId":1770},{"id":76,"name":"意甲部落","icon":"http://cdn.qiudd.net/52_1487486418_1487486407994.png","cover":"http://cdn.qiudd.net/52_1487486418_1487486412386.jpg","groupType":1,"teamId":0}],"6":[{"id":47,"name":"巴黎圣日耳曼","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/886_f4732928feada8d6021f73044c4d05dc.png","cover":"http://cdn.qiudd.net/4_1488955092_1488955104391.jpg","groupType":1,"teamId":146},{"id":49,"name":"摩纳哥","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/885_e067d0f79b2362ca28d57785cd2fcc72.png","cover":"http://cdn.qiudd.net/4_1488955116_1488955122819.jpg","groupType":1,"teamId":1870},{"id":67,"name":"尼斯","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/894_573e78fc513c85b8f468a2a979d85316.png","cover":"http://cdn.qiudd.net/4_1488955124_1488955131829.jpg","groupType":1,"teamId":1815},{"id":78,"name":"法甲部落","icon":"http://cdn.qiudd.net/52_1487489265_1487489249136.png","cover":"http://cdn.qiudd.net/52_1487489265_1487489253523.jpg","groupType":1,"teamId":0}],"7":[{"id":3,"name":"话题区","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/77_1473391954_1473391960278.png","cover":"http://cdn.qiudd.net/52_1482291715_1482292716021.jpg","groupType":1,"teamId":0},{"id":64,"name":"超级颜论","icon":"http://cdn.qiudd.net/52_1493975163_1493976297686.jpg","cover":"http://cdn.qiudd.net/52_1479105067_1479105049023.jpg","groupType":1,"teamId":0},{"id":50,"name":"用户反馈","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/77_1473395212_1473395218045.png","cover":"http://cdn.qiudd.net/45_1475471438_1475471446535.jpg","groupType":1,"teamId":0},{"id":52,"name":"撩闲","icon":"http://cdn.qiudd.net/52_1482375665_1482375659259.png","cover":"http://cdn.qiudd.net/52_1482374403_1482374406351.jpg","groupType":1,"teamId":0},{"id":53,"name":"中国","icon":"http://cdn.qiudd.net/83_1496821870_1496821873939.png","cover":"","groupType":1,"teamId":1715},{"id":23,"name":"曼联","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/662_c83f70cde952353e3bd25daf96b0972d.png","cover":"http://cdn.qiudd.net/4_1488358706_1488358716679.jpg","groupType":1,"teamId":143},{"id":24,"name":"曼城","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/45_1468988657_1468988669263.png","cover":"http://cdn.qiudd.net/4_1488358722_1488358726838.jpg","groupType":1,"teamId":145},{"id":25,"name":"切尔西","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/661_615274b59dd4e7749cdce02d0e5ea639.png","cover":"http://cdn.qiudd.net/4_1488358732_1488358736635.jpg","groupType":1,"teamId":142},{"id":26,"name":"阿森纳","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/660_5d4beb410e8b3da7316dbf8cf333b455.png","cover":"http://cdn.qiudd.net/4_1488358639_1488358662297.jpg","groupType":1,"teamId":141},{"id":27,"name":"利物浦","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/663_be794e2758f3087f637d2122fb3d8c3e.png","cover":"http://cdn.qiudd.net/4_1488358744_1488358750667.jpg","groupType":1,"teamId":399},{"id":30,"name":"皇家马德里","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/2016_f7233dcda8045400203254694da6dffb.png","cover":"http://cdn.qiudd.net/4_1488358794_1488358800683.jpg","groupType":1,"teamId":151},{"id":31,"name":"巴塞罗那","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/2017_bab20eb5fb3bd3073beca64ce07c1496.png","cover":"http://cdn.qiudd.net/4_1488358807_1488358815614.jpg","groupType":1,"teamId":152},{"id":36,"name":"拜仁慕尼黑","icon":"http://cdn.qiudd.net/52_1498650007_1498650014238.png","cover":"http://cdn.qiudd.net/52_1498648286_1498648394178.jpg","groupType":1,"teamId":2},{"id":40,"name":"AC米兰","icon":"http://cdn.qiudd.net/52_1478660777_1478660756963.png","cover":"http://cdn.qiudd.net/4_1488359221_1488359226588.jpg","groupType":1,"teamId":147},{"id":41,"name":"国际米兰","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/45_1469167738_1469167745584.png","cover":"http://cdn.qiudd.net/4_1488359238_1488359279429.jpg","groupType":1,"teamId":150},{"id":42,"name":"尤文图斯","icon":"http://cdn.qiudd.net/52_1487818567_1487818660687.png","cover":"http://cdn.qiudd.net/4_1488955157_1488955167036.jpg","groupType":1,"teamId":149},{"id":65,"name":"图说足球","icon":"http://cdn.qiudd.net/52_1480042672_1480042645912.jpg","cover":"http://cdn.qiudd.net/52_1480041414_1480041404479.jpg","groupType":1,"teamId":0},{"id":67,"name":"尼斯","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo/894_573e78fc513c85b8f468a2a979d85316.png","cover":"http://cdn.qiudd.net/4_1488955124_1488955131829.jpg","groupType":1,"teamId":1815},{"id":69,"name":"足球装备","icon":"http://cdn.qiudd.net/52_1483582980_1483582979525.png","cover":"http://cdn.qiudd.net/52_1483579719_1483579769226.jpg","groupType":1,"teamId":0},{"id":70,"name":"足球问答","icon":"http://cdn.qiudd.net/52_1483582990_1483582990306.png","cover":"http://cdn.qiudd.net/52_1483579787_1483579812014.jpg","groupType":1,"teamId":0},{"id":81,"name":"欧冠联赛","icon":"http://cdn.qiudd.net/52_1488692069_1488692093004.png","cover":"http://cdn.qiudd.net/52_1488690888_1488690924382.jpg","groupType":1,"teamId":0},{"id":83,"name":"技战术讨论","icon":"http://cdn.qiudd.net/52_1488690972_1488690980131.png","cover":"http://cdn.qiudd.net/52_1488690972_1488690986631.jpg","groupType":1,"teamId":0},{"id":66,"name":"为了部落","icon":"http://cdn.qiudd.net/52_1481785958_1481787344618.png","cover":"http://cdn.qiudd.net/52_1481785958_1481785971579.jpg","groupType":1,"teamId":0},{"id":91,"name":"中甲联赛","icon":"http://cdn.qiudd.net/52_1494210994_1494210997417.jpg","cover":"http://cdn.qiudd.net/52_1494210581_1494210609336.jpg","groupType":1,"teamId":0},{"id":59,"name":"足球游戏","icon":"http://cdn.qiudd.net/52_1494211408_1494212193752.jpg","cover":"http://cdn.qiudd.net/52_1494211408_1494212200344.jpg","groupType":1,"teamId":0},{"id":96,"name":"欧联杯","icon":"http://cdn.qiudd.net/52_1496631250_1496631642457.png","cover":"http://cdn.qiudd.net/52_1496631250_1496631658345.jpg","groupType":1,"teamId":0},{"id":97,"name":"中超联赛","icon":"http://cdn.qiudd.net/52_1496634099_1496634104297.png","cover":"http://cdn.qiudd.net/52_1496634099_1496634111689.jpg","groupType":1,"teamId":0},{"id":100,"name":"球星卡","icon":"http://cdn.qiudd.net/52_1501905013_1501905018662.png","cover":"http://cdn.qiudd.net/52_1501905013_1501905026005.jpg","groupType":1,"teamId":0},{"id":102,"name":"世界杯","icon":"http://cdn.qiudd.net/FkpmE-fd9kSXB-CwjdvFrJU0Ykbm.png","cover":"http://cdn.qiudd.net/FkMTTotIp7dkMHyqmVvkCJmFBRR-.jpg","groupType":1,"teamId":0},{"id":105,"name":"踢球者","icon":"http://cdn.qiudd.net/FpvPbcKdIOUlfhoesOxupnVMku34.png","cover":"http://cdn.qiudd.net/FhLpA6bgxABbHX0HvVk_kReKW2xd.jpg","groupType":1,"teamId":0},{"id":68,"name":"GIF","icon":"http://cdn.qiudd.net/52_1483582967_1483582967248.png","cover":"http://cdn.qiudd.net/52_1483579395_1483579697049.jpg","groupType":1,"teamId":0}],"8":[{"id":3,"name":"话题区","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/77_1473391954_1473391960278.png","cover":"http://cdn.qiudd.net/52_1482291715_1482292716021.jpg","groupType":1,"teamId":0},{"id":91,"name":"中甲联赛","icon":"http://cdn.qiudd.net/52_1494210994_1494210997417.jpg","cover":"http://cdn.qiudd.net/52_1494210581_1494210609336.jpg","groupType":1,"teamId":0},{"id":52,"name":"撩闲","icon":"http://cdn.qiudd.net/52_1482375665_1482375659259.png","cover":"http://cdn.qiudd.net/52_1482374403_1482374406351.jpg","groupType":1,"teamId":0},{"id":50,"name":"用户反馈","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/77_1473395212_1473395218045.png","cover":"http://cdn.qiudd.net/45_1475471438_1475471446535.jpg","groupType":1,"teamId":0},{"id":66,"name":"为了部落","icon":"http://cdn.qiudd.net/52_1481785958_1481787344618.png","cover":"http://cdn.qiudd.net/52_1481785958_1481785971579.jpg","groupType":1,"teamId":0},{"id":1,"name":"世界播","icon":"http://cdn.qiudd.net/52_1475066090_1475066096787.png","cover":"http://cdn.qiudd.net/52_1482721023_1482721392738.jpg","groupType":3,"teamId":0},{"id":2,"name":"有妹子","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/77_1473391452_1473391458351.png","cover":"http://cdn.qiudd.net/52_1482294936_1482295038306.jpg","groupType":2,"teamId":0},{"id":51,"name":"天天竞猜","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/77_1473395326_1473395333755.png","cover":"http://cdn.qiudd.net/45_1475470670_1475470681551.jpg","groupType":1,"teamId":0},{"id":58,"name":"女足部落","icon":"http://cdn.qiudd.net/52_1501905126_1501906508796.png","cover":"http://cdn.qiudd.net/52_1501905126_1501906517501.jpg","groupType":1,"teamId":0},{"id":59,"name":"足球游戏","icon":"http://cdn.qiudd.net/52_1494211408_1494212193752.jpg","cover":"http://cdn.qiudd.net/52_1494211408_1494212200344.jpg","groupType":1,"teamId":0},{"id":65,"name":"图说足球","icon":"http://cdn.qiudd.net/52_1480042672_1480042645912.jpg","cover":"http://cdn.qiudd.net/52_1480041414_1480041404479.jpg","groupType":1,"teamId":0},{"id":68,"name":"GIF","icon":"http://cdn.qiudd.net/52_1483582967_1483582967248.png","cover":"http://cdn.qiudd.net/52_1483579395_1483579697049.jpg","groupType":1,"teamId":0},{"id":69,"name":"足球装备","icon":"http://cdn.qiudd.net/52_1483582980_1483582979525.png","cover":"http://cdn.qiudd.net/52_1483579719_1483579769226.jpg","groupType":1,"teamId":0},{"id":70,"name":"足球问答","icon":"http://cdn.qiudd.net/52_1483582990_1483582990306.png","cover":"http://cdn.qiudd.net/52_1483579787_1483579812014.jpg","groupType":1,"teamId":0},{"id":74,"name":"影音赏析","icon":"http://cdn.qiudd.net/52_1486344316_1486344301616.png","cover":"http://cdn.qiudd.net/52_1486344316_1486344304677.jpg","groupType":1,"teamId":0},{"id":75,"name":"冷门足球","icon":"http://cdn.qiudd.net/52_1486344333_1486344318125.png","cover":"http://cdn.qiudd.net/52_1486345326_1486345307037.jpg","groupType":1,"teamId":0},{"id":81,"name":"欧冠联赛","icon":"http://cdn.qiudd.net/52_1488692069_1488692093004.png","cover":"http://cdn.qiudd.net/52_1488690888_1488690924382.jpg","groupType":1,"teamId":0},{"id":82,"name":"表情包部落","icon":"http://cdn.qiudd.net/52_1488690947_1488690955977.png","cover":"http://cdn.qiudd.net/52_1488690947_1488690960304.jpg","groupType":1,"teamId":0},{"id":83,"name":"技战术讨论","icon":"http://cdn.qiudd.net/52_1488690972_1488690980131.png","cover":"http://cdn.qiudd.net/52_1488690972_1488690986631.jpg","groupType":1,"teamId":0},{"id":84,"name":"读书品卷","icon":"http://cdn.qiudd.net/52_1491360122_1491360144146.png","cover":"http://cdn.qiudd.net/52_1491360122_1491360155706.jpg","groupType":1,"teamId":0},{"id":96,"name":"欧联杯","icon":"http://cdn.qiudd.net/52_1496631250_1496631642457.png","cover":"http://cdn.qiudd.net/52_1496631250_1496631658345.jpg","groupType":1,"teamId":0},{"id":98,"name":"肆客游","icon":"http://cdn.qiudd.net/FvoreX5MmoPkftjcaDYwWPZxKYI8.png","cover":"http://cdn.qiudd.net/FpYH1UqBOYcQWjejUd-HRd5W1A3s.jpg","groupType":1,"teamId":0},{"id":99,"name":"足坛往事","icon":"http://cdn.qiudd.net/52_1499225229_1499225237663.png","cover":"http://cdn.qiudd.net/52_1499225229_1499225242340.jpg","groupType":1,"teamId":0},{"id":100,"name":"球星卡","icon":"http://cdn.qiudd.net/52_1501905013_1501905018662.png","cover":"http://cdn.qiudd.net/52_1501905013_1501905026005.jpg","groupType":1,"teamId":0},{"id":101,"name":"FM部落","icon":"http://cdn.qiudd.net/FkOUDyQAKD8aSP79CkIOZhf1X6Qp.png","cover":"http://cdn.qiudd.net/FlVLyAAdRfYekj8N35pXUrsjQ7aI.jpg","groupType":1,"teamId":0},{"id":105,"name":"踢球者","icon":"http://cdn.qiudd.net/FpvPbcKdIOUlfhoesOxupnVMku34.png","cover":"http://cdn.qiudd.net/FhLpA6bgxABbHX0HvVk_kReKW2xd.jpg","groupType":1,"teamId":0}],"9":[{"id":102,"name":"世界杯","icon":"http://cdn.qiudd.net/FkpmE-fd9kSXB-CwjdvFrJU0Ykbm.png","cover":"http://cdn.qiudd.net/FkMTTotIp7dkMHyqmVvkCJmFBRR-.jpg","groupType":1,"teamId":0},{"id":85,"name":"阿根廷","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo0530/132_c1bcf3ed68fecf9e17f41a4df25fb903.png","cover":"","groupType":1,"teamId":1783},{"id":86,"name":"巴西","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo0530/349_64a374c08a9260eee9559869bcba39f1.png","cover":"","groupType":1,"teamId":1785},{"id":88,"name":"德国","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo0530/1037_761933aa79e4280bc3c3d74f7e830549.png","cover":"","groupType":1,"teamId":270},{"id":89,"name":"葡萄牙","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo0530/1772_e304ac18ebc8ee2d29ee8b338e00ab9c.png","cover":"","groupType":1,"teamId":289},{"id":90,"name":"意大利","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo0530/1318_1ea1bc6ffedc8b51ffcce5af4bf03988.png","cover":"","groupType":1,"teamId":276},{"id":92,"name":"英格兰","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/45_1464925742_1464925745529.png","cover":"","groupType":1,"teamId":264},{"id":93,"name":"西班牙","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo0530/2137_f055d53c2a2710f86118575374e4c863.png","cover":"","groupType":1,"teamId":295},{"id":94,"name":"法国","icon":"http://7xpd1v.com2.z0.glb.qiniucdn.com/logo0530/944_5e84a7cb93a803547194bbeba456e7ff.png","cover":"","groupType":1,"teamId":268},{"id":53,"name":"中国","icon":"http://cdn.qiudd.net/83_1496821870_1496821873939.png","cover":"","groupType":1,"teamId":1715}]}},"serverTime":1517927174439}

数据太长,不适合观看,可以尝试下这个链接http://api.qiuduoduo.cn/sns/categories/all?hasGroups=true

动态变化的Key

由上图可以看出来categoryGroupsMap对象下面的是一个1,2,3,4,5动态变化的Key对象。面对这种Key我可以采用下面这种解决方案

package com.wj.baseutils.bean;

import java.util.List;
import java.util.Map;

/**
 * Created by wj on 2018/2/5.
 */

public class TribeCategoryBean {

    public int ret;
    public DataBean data;
    public long serverTime;

    public static class DataBean {

        //        public CategoryGroupsMapBean categoryGroupsMap;
        public List<CategoriesBean> categories;
        public Map<String, List<CategoryGroupsMapBean.ObjBean>> categoryGroupsMap;

        public static class CategoryGroupsMapBean {

            public static class ObjBean {

                public int id;
                public String name;
                public String icon;
                public String cover;
                public int groupType;
                public int teamId;
            }
        }

        public static class CategoriesBean {

            public int id;
            public String name;
        }
    }
}

从上面代码中,我们可以看出CategoryGroupsMapBean我们定义成了一个Map集合,Map的String类型的Key正好对应动态变化的Key。

下面获取Map对象,并且for循环遍历,获取key从Map中获取我们想要的Value,也就是我们需要的对象~~~~~~~~

@Override
    public void setCategory(TribeCategoryBean result) {
        typeList.clear();
        list.clear();
        if (result != null && result.data != null
                && result.data.categories != null &&
                result.data.categories.size() > 0) {

            typeList.addAll(result.data.categories);
            typeAdapter.notifyDataSetChanged();

            Map<String, List<TribeCategoryBean.DataBean.CategoryGroupsMapBean.ObjBean>> categoryGroupsMap =
                    result.data.categoryGroupsMap;
            for (String key : categoryGroupsMap.keySet()) {
                List<TribeCategoryBean.DataBean.CategoryGroupsMapBean.ObjBean> objBeans = categoryGroupsMap.get(key);
            }

        }
    }
动态变化的Key

附上完整的项目代码Link

相关文章

网友评论

    本文标题:Android中Gson、FastJson解析动态的Key

    本文链接:https://www.haomeiwen.com/subject/gpvnzxtx.html