写在前面的话,本文章只适合使用AndroidStudio的小伙伴观看,还在纠结eclipse的小伙伴赶紧洗洗睡吧。。。
最近看见一篇快速实现javaBean的屎丢丢插件,这是一个根据JSONObject格式的字符串,自动生成实体类参数。
如果想要使用这个插件,有两个方法可供选择。
安装方法
方法一:
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 格式
{
“name”: “王五”,
“gender”: “man”,
“age”: 15,
“height”: “140cm”,
}
/**
* Created by thinkwyp on 2015/6/1.
*/
public class TestBean {
/**
* gender : man
* name : 王五
* age : 15
* height : 140cm
*/
private String gender;
private String name;
private int age;
private String height;
public void setGender(String gender) {
this.gender = gender;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setHeight(String height) {
this.height = height;
}
public String getGender() {
return gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getHeight() {
return height;
}
}
复杂的实体类
实体类不仅包含另外一个实体,还包含另外实体的数组.
图中复杂的json 格式
{
“name”: “王五”,
“gender”: “man”,
“age”: 15,
“height”: “140cm”,
“addr”: {
“province”: “fujian”,
“city”: “quanzhou”,
“code”: “300000”
},
“hobby”: [
{
“name”: “billiards”,
“code”: “1”
},
{
“name”: “computerGame”,
“code”: “2”
}
]
}
实体类
/**
* Created by thinkwyp on 2015/6/1.
*/
public class Bean extends JSONModel {
/**
* height : 140cm
* age : 15
* name : 王五
* hobby : [{"name":"billiards","code":"1"},{"name":"computerGame","code":"2"}]
* gender : man
* addr : {"province":"fujian","code":"300000","city":"quanzhou"}
*/
private String height;
private int age;
private String name;
private List<HobbyEntity> hobby;
private String gender;
private AddrEntity addr;
public void setHeight(String height) {
this.height = height;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setHobby(List<HobbyEntity> hobby) {
this.hobby = hobby;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setAddr(AddrEntity addr) {
this.addr = addr;
}
public String getHeight() {
return height;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public List<HobbyEntity> getHobby() {
return hobby;
}
public String getGender() {
return gender;
}
public AddrEntity getAddr() {
return addr;
}
public class HobbyEntity {
/**
* name : billiards
* code : 1
*/
private String name;
private String code;
public void setName(String name) {
this.name = name;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public String getCode() {
return code;
}
}
public class AddrEntity {
/**
* province : fujian
* code : 300000
* city : quanzhou
*/
private String province;
private String code;
private String city;
public void setProvince(String province) {
this.province = province;
}
public void setCode(String code) {
this.code = code;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public String getCode() {
return code;
}
public String getCity() {
return city;
}
}
}
这个东西还支持两种注释过滤
/**段注释:
*/
//行注释:
例子
{
/**
* 名字
*/
“name”: “王五”,
“gender”: “man”,//性别
“age”: 15,
“height”: “140cm”,
}
这个酱紫也是可以解析的,今早亲测成功,有用到的同学就拿去用吧
离线插件下载地址
GsonFormat.jar