插件GsonFormat快速实现JavaBean
Posted Sharley
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了插件GsonFormat快速实现JavaBean相关的知识,希望对你有一定的参考价值。
转自:http://blog.csdn.net/dakaring/article/details/46300963#comments
安装方法
方法一:
1.Android studio File->Settings..->Plugins–>Browse repositores..搜索GsonFormat
2.安装插件,重启android studio
方法二:
1.下载GsonFormat.jar ;
2.Android studio File->Settings..->Plugins –>install plugin from disk..导入下载GsonFormat.jar
3.重启android studio
使用方法
在实体类中使用Generate的快捷键.
快捷键:图中选中的部分
简单的实体类
图中简单的 json 格式
1 { 2 “name”: “王五”, 3 “gender”: “man”, 4 “age”: 15, 5 “height”: “140cm”, 6 } 7 8 /** 9 * Created by thinkwyp on 2015/6/1. 10 */ 11 public class TestBean { 12 13 14 /** 15 * gender : man 16 * name : 王五 17 * age : 15 18 * height : 140cm 19 */ 20 private String gender; 21 private String name; 22 private int age; 23 private String height; 24 25 public void setGender(String gender) { 26 this.gender = gender; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public void setAge(int age) { 34 this.age = age; 35 } 36 37 public void setHeight(String height) { 38 this.height = height; 39 } 40 41 public String getGender() { 42 return gender; 43 } 44 45 public String getName() { 46 return name; 47 } 48 49 public int getAge() { 50 return age; 51 } 52 53 public String getHeight() { 54 return height; 55 } 56 }
复杂的实体类
实体类不仅包含另外一个实体,还包含另外实体的数组.
图中复杂的json 格式
{
“name”: “王五”,
“gender”: “man”,
“age”: 15,
“height”: “140cm”,
“addr”: {
“province”: “fujian”,
“city”: “quanzhou”,
“code”: “300000”
},
“hobby”: [
{
“name”: “billiards”,
“code”: “1”
},
{
“name”: “computerGame”,
“code”: “2”
}
]
}
实体类
1 /** 2 * Created by thinkwyp on 2015/6/1. 3 */ 4 public class Bean extends JSONModel { 5 6 7 /** 8 * height : 140cm 9 * age : 15 10 * name : 王五 11 * hobby : [{"name":"billiards","code":"1"},{"name":"computerGame","code":"2"}] 12 * gender : man 13 * addr : {"province":"fujian","code":"300000","city":"quanzhou"} 14 */ 15 private String height; 16 private int age; 17 private String name; 18 private List<HobbyEntity> hobby; 19 private String gender; 20 private AddrEntity addr; 21 22 public void setHeight(String height) { 23 this.height = height; 24 } 25 26 public void setAge(int age) { 27 this.age = age; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public void setHobby(List<HobbyEntity> hobby) { 35 this.hobby = hobby; 36 } 37 38 public void setGender(String gender) { 39 this.gender = gender; 40 } 41 42 public void setAddr(AddrEntity addr) { 43 this.addr = addr; 44 } 45 46 public String getHeight() { 47 return height; 48 } 49 50 public int getAge() { 51 return age; 52 } 53 54 public String getName() { 55 return name; 56 } 57 58 public List<HobbyEntity> getHobby() { 59 return hobby; 60 } 61 62 public String getGender() { 63 return gender; 64 } 65 66 public AddrEntity getAddr() { 67 return addr; 68 } 69 70 public class HobbyEntity { 71 /** 72 * name : billiards 73 * code : 1 74 */ 75 private String name; 76 private String code; 77 78 public void setName(String name) { 79 this.name = name; 80 } 81 82 public void setCode(String code) { 83 this.code = code; 84 } 85 86 public String getName() { 87 return name; 88 } 89 90 public String getCode() { 91 return code; 92 } 93 } 94 95 public class AddrEntity { 96 /** 97 * province : fujian 98 * code : 300000 99 * city : quanzhou 100 */ 101 private String province; 102 private String code; 103 private String city; 104 105 public void setProvince(String province) { 106 this.province = province; 107 } 108 109 public void setCode(String code) { 110 this.code = code; 111 } 112 113 public void setCity(String city) { 114 this.city = city; 115 } 116 117 public String getProvince() { 118 return province; 119 } 120 121 public String getCode() { 122 return code; 123 } 124 125 public String getCity() { 126 return city; 127 } 128 } 129 }
这个东西还支持两种注释过滤
/**段注释:
*/
//行注释:
例子
{
/**
* 名字
*/
“name”: “王五”,
“gender”: “man”,//性别
“age”: 15,
“height”: “140cm”,
}
这个酱紫也是可以解析的,今早亲测成功,有用到的同学就拿去用吧
离线插件下载地址
GsonFormat.jar
以上是关于插件GsonFormat快速实现JavaBean的主要内容,如果未能解决你的问题,请参考以下文章
Android studio 插件之 GsonFormat (自动生成javabean)