概述
相信大家在做开发的过程中都写过非常多的javabean ,非常多情况下 都是一个列表数据就是一个单独的javabean。假设大家自己敲的话费时费力 还非常easy敲错。
今天给大家推荐一个插件 GsonFormat 这个插件就是能够让我们直接将服务端返回的json转化为实体类。Android studio和idea都有, eclipse我没有试过。
大家能够找找有没有。
操作步骤
第一步 下载插件
下载插件的过程我前面有一篇博客已经有具体步骤 Android Studio插件之sexy editor(设置AS背景) 大家 仅仅须要将那篇博客中介绍的插件名称换成GsonFormat
依照那个步骤下载好 插件。记得下载安装好后要重新启动哦。
第二步 自己主动生成bean
以下我们介绍怎么通过这个插件将服务端返回的json自己主动生成javabean
首先 我们要建一个空的java类,然后 在空白出点击鼠标右键 选择 Generate... 假设你已经安装成功好插件会弹出以下这个框
选择GsonFormat会弹出以下这个框
将服务端接返回的json 粘贴到这个里面 点击ok
粘贴完毕后 点击ok会弹出例如以下图所看到的的选择 字段界面
这个界面是让你自己选择 保留哪些字段。可能有的字段不须要 就把前面的对勾去掉就能够了。最后点击ok。大功告成。
贴上json字符串和自己主动生成出的bean代码.
json字符串
<span style="font-size:14px;">{"retval":"ok","reterr":"","retinfo":{"count":2,"groups":[{"groupid":1,"title":"u65b0u670bu53cb","icon":"http://aliimg.yizhibo.tv/online/message/2f/74/hi.png","type":"1","readcnt":67,"unread":2,"total":19,"update_time":"2016-04-05 09:11:09","lastest_content":{"type":2,"data":{"name":"16837286","nickname":"u963fu83b2","gender":"female","signature":"","vip":"0","logourl":"http://aliimg.yizhibo.tv/online/user/2c/17/60ec3f44e8a0de9db1d87f1a59875ce1.jpg@100h_100w_90Q_0e_1c"}}},{"groupid":0,"title":"u6613u76f4u64adu5c0fu79d8u4e66","icon":"http://aliimg.yizhibo.tv/online/message/07/fd/Secretary.png","type":0,"readcnt":86,"unread":0,"total":86,"update_time":"2016-04-01 00:09:28","lastest_content":{"type":0,"data":{"text":"u611au4ebau8282u6765u4e34u4e4bu9645uff0cu5fc3u4e2du7684u5c0fu6076u9b54u6b63u5728u9a9au52a8u3002u6613u76f4u64adu611au4ebau8282u65e0u654cu5927u738bu4e89u9738u96c6u7ed3u53f7u4e5fu501fu52bfu90aau6076u5439u54cduff01u770bu5404u5927u5bb6u5982u4f55u5927u65bdu8ff7u9b42u9635uff0cu6597u5fd7u6597u52c7uff0cu620eu9a6cu800cu5f52u3002u201cu611au4ebau5927u738bu201du3001u201cu8303u4e8cu5927u738bu201du7a76u7adfu82b1u843du8c01u5bb6u5462uff1fu54b1u4eecu62edu76eeu4ee5u5f85uff01"}}}]}}</span>
生成的bean
<span style="font-size:14px;"> import java.util.List; public class MessageGroupEntity { /** * retval : ok * reterr : * retinfo : {"count":2,"groups":[{"groupid":1,"title":"新朋友", * "icon":"http://aliimg.yizhibo.tv/online/message/2f/74/hi.png","type":"1","readcnt":67, * "unread":2,"total":19,"update_time":"2016-04-05 09:11:09","lastest_content":{"type":2, * "data":{"name":"16837286","nickname":"阿莲","gender":"female","signature":"","vip":"0", * "logourl":"http://aliimg.yizhibo.tv/online/user/2c/17/60ec3f44e8a0de9db1d87f1a59875ce1.jpg@100h_100w_90Q_0e_1c"}}} * ,{"groupid":0,"title":"易直播小秘书","icon":"http://aliimg.yizhibo.tv/online/message/07/fd/Secretary.png","type":0,"readcnt":86, * "unread":0,"total":86,"update_time":"2016-04-01 00:09:28", * "lastest_content":{"type":0,"data":{"text":"愚人节来临之际。心中的小恶魔正在骚动。易直播愚人节无敌大王争霸集结号也借势邪恶吹响! * 看各大家怎样大施迷魂阵,斗志斗勇,戎马而归。u201c愚人大王u201d、u201c范二大王u201d到底花落谁家呢?咱们拭目以待!"}}}]} */ private String retval; private String reterr; /** * count : 2 * groups : [{"groupid":1,"title":"新朋友", * "icon":"http://aliimg.yizhibo.tv/online/message/2f/74/hi.png","type":"1","readcnt":67, * "unread":2,"total":19,"update_time":"2016-04-05 09:11:09", * "lastest_content":{"type":2,"data":{"name":"16837286","nickname":"阿莲","gender":"female", * "signature":"","vip":"0","logourl":"http://aliimg.yizhibo.tv/online/user/2c/17/60ec3f44e8a0de9db1d87f1a59875ce1.jpg@100h_100w_90Q_0e_1c"}}}, * {"groupid":0,"title":"易直播小秘书","icon":"http://aliimg.yizhibo.tv/online/message/07/fd/Secretary.png", * "type":0,"readcnt":86,"unread":0,"total":86,"update_time":"2016-04-01 00:09:28","lastest_content": * {"type":0,"data":{"text":"愚人节来临之际,心中的小恶魔正在骚动。易直播愚人节无敌大王争霸集结号也借势邪恶吹响!看各大家怎样大施迷魂阵。斗志斗勇。戎马而归。 * u201c愚人大王u201d、u201c范二大王u201d到底花落谁家呢?咱们拭目以待!"}}}] */ private RetinfoEntity retinfo; public void setRetval(String retval) { this.retval = retval; } public void setReterr(String reterr) { this.reterr = reterr; } public void setRetinfo(RetinfoEntity retinfo) { this.retinfo = retinfo; } public String getRetval() { return retval; } public String getReterr() { return reterr; } public RetinfoEntity getRetinfo() { return retinfo; } public static class RetinfoEntity { private int count; /** * groupid : 1 * title : 新朋友 * icon : http://aliimg.yizhibo.tv/online/message/2f/74/hi.png * type : 1 * readcnt : 67 * unread : 2 * total : 19 * update_time : 2016-04-05 09:11:09 * lastest_content : {"type":2,"data":{"name":"16837286","nickname":"阿莲","gender":"female","signature":"","vip":"0","logourl":"http://aliimg.yizhibo.tv/online/user/2c/17/60ec3f44e8a0de9db1d87f1a59875ce1.jpg@100h_100w_90Q_0e_1c"}} */ private List<GroupsEntity> groups; public void setCount(int count) { this.count = count; } public void setGroups(List<GroupsEntity> groups) { this.groups = groups; } public int getCount() { return count; } public List<GroupsEntity> getGroups() { return groups; } public static class GroupsEntity { private int groupid; private String title; private String icon; private String type; private int readcnt; private int unread; private int total; private String update_time; /** * type : 2 * data : {"name":"16837286","nickname":"阿莲","gender":"female","signature":"","vip":"0","logourl":"http://aliimg.yizhibo.tv/online/user/2c/17/60ec3f44e8a0de9db1d87f1a59875ce1.jpg@100h_100w_90Q_0e_1c"} */ private LastestContentEntity lastest_content; public void setGroupid(int groupid) { this.groupid = groupid; } public void setTitle(String title) { this.title = title; } public void setIcon(String icon) { this.icon = icon; } public void setType(String type) { this.type = type; } public void setReadcnt(int readcnt) { this.readcnt = readcnt; } public void setUnread(int unread) { this.unread = unread; } public void setTotal(int total) { this.total = total; } public void setUpdate_time(String update_time) { this.update_time = update_time; } public void setLastest_content(LastestContentEntity lastest_content) { this.lastest_content = lastest_content; } public int getGroupid() { return groupid; } public String getTitle() { return title; } public String getIcon() { return icon; } public String getType() { return type; } public int getReadcnt() { return readcnt; } public int getUnread() { return unread; } public int getTotal() { return total; } public String getUpdate_time() { return update_time; } public LastestContentEntity getLastest_content() { return lastest_content; } public static class LastestContentEntity { private int type; /** * name : 16837286 * nickname : 阿莲 * gender : female * signature : * vip : 0 * logourl : http://aliimg.yizhibo.tv/online/user/2c/17/60ec3f44e8a0de9db1d87f1a59875ce1.jpg@100h_100w_90Q_0e_1c */ private DataEntity data; public void setType(int type) { this.type = type; } public void setData(DataEntity data) { this.data = data; } public int getType() { return type; } public DataEntity getData() { return data; } public static class DataEntity { private String name; private String nickname; private String gender; private String signature; private String vip; private String logourl; public void setName(String name) { this.name = name; } public void setNickname(String nickname) { this.nickname = nickname; } public void setGender(String gender) { this.gender = gender; } public void setSignature(String signature) { this.signature = signature; } public void setVip(String vip) { this.vip = vip; } public void setLogourl(String logourl) { this.logourl = logourl; } public String getName() { return name; } public String getNickname() { return nickname; } public String getGender() { return gender; } public String getSignature() { return signature; } public String getVip() { return vip; } public String getLogourl() { return logourl; } } } } } } </span>
大概流程就是这样,希望对大家有所帮助。
欢迎大家增加我的qq 群:454430053