您的位置:首页 > 移动开发 > Android开发

还在手动写Bean?赶紧来用GsonFormat吧

2017-12-10 16:39 274 查看
在Android开发中避免不了和Server端的童鞋打交道,请求接口以后Server端一般返回的都是一个Json串信息,我们拿到Json串然后通过JsonObject或者JsonArray转换成我们需要的Bean,项目中我们一般使用Google的Gson或者阿里的FastJson去处理Json串,他们都是通过反射将Json串转换成Bean,那么我们需要做的就是根据返回的Json串去写对应的Bean类,比如Server给我们传回来的Json串是这样滴:

{
"Category": [
{
"categoryId": 1,
"categoryName": "饮品",
"categoryImage": "/upload/yinpin.jpg"
},
{
"categoryId": 2,
"categoryName": "食品",
"categoryImage": "/upload/shiping.jpg"
},
{
"categoryId": 3,
"categoryName": "酒类",
"categoryImage": "/upload/jiullei.jpg"
}
],
"recommend": {
"id": 11,
"productName": "统一老坛泡椒牛肉袋面香辣味110g*24袋",
"filenameSmall": "/upload/ty_ltpj_small.jpg",
"productPrice": 48,
"productCost": 47.5
}
}


我们需要根据上面的Json串来写出对应的Bean类,看得出上面的字段还是不少的,所以把所有的字段都写出来还是有一定的工作量的,而且手工写容易出错,一旦字段不对应,可能会导致整个解析过程出错,这时候GsonFormat迫不及待地跳出来:用我,用我!

GsonFormat是一个Android Studio中的插件,它可以大大提高我们生成Bean类时的速度,下面就来看一下用GsonFormat来生成Bean的过程。

1、首先去Android Studio的设置中找到Plugins界面,如下图:



2、在输入框中搜索GsonFormat,点击Browse repositories…,就到了下面这个界面:



3、点击install安装并重启一下AS,就可以使用这个插件了,在Bean类的空白处点击,选择Generate…,如下图:



4、在输入框中输入Server端返回的Json串,点击OK:



5、因为示例中的Json串会产生两个内部类,GsonFormat已经给我们推荐了内部类的名字,我们继续点OK按钮就可以了:



最后看一下生成结果:

public class noodleBean {

/**
* Category : [{"categoryId":1,"categoryName":"饮品","categoryImage":"/upload/yinpin.jpg"},{"categoryId":2,"categoryName":"食品","categoryImage":"/upload/shiping.jpg"},{"categoryId":3,"categoryName":"酒类","categoryImage":"/upload/jiullei.jpg"}]
* recommend : {"id":11,"productName":"统一老坛泡椒牛肉袋面香辣味110g*24袋","filenameSmall":"/upload/ty_ltpj_small.jpg","productPrice":48,"productCost":47.5}
*/

private RecommendBean recommend;
private List<CategoryBean> Category;

public RecommendBean getRecommend() {
return recommend;
}

public void setRecommend(RecommendBean recommend) {
this.recommend = recommend;
}

public List<CategoryBean> getCategory() {
return Category;
}

public void setCategory(List<CategoryBean> Category) {
this.Category = Category;
}

public static class RecommendBean {
/**
* id : 11
* productName : 统一老坛泡椒牛肉袋面香辣味110g*24袋
* filenameSmall : /upload/ty_ltpj_small.jpg
* productPrice : 48
* productCost : 47.5
*/

private int id;
private String productName;
private String filenameSmall;
private int productPrice;
private double productCost;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getProductName() {
return productName;
}

public void setProductName(String productName) {
this.productName = productName;
}

public String getFilenameSmall() {
return filenameSmall;
}

public void setFilenameSmall(String filenameSmall) {
this.filenameSmall = filenameSmall;
}

public int getProductPrice() {
return productPrice;
}

public void setProductPrice(int productPrice) {
this.productPrice = productPrice;
}

public double getProductCost() {
return productCost;
}

public void setProductCost(double productCost) {
this.productCost = productCost;
}
}

public static class CategoryBean {
/**
* categoryId : 1
* categoryName : 饮品
* categoryImage : /upload/yinpin.jpg
*/

private int categoryId;
private String categoryName;
private String categoryImage;

public int getCategoryId() {
return categoryId;
}

public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}

public String getCategoryName() {
return categoryName;
}

public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}

public String getCategoryImage() {
return categoryImage;
}

public void setCategoryImage(String categoryImage) {
this.categoryImage = categoryImage;
}
}
}


怎么样?比我们手写快多了吧,而且不会出错,不过我们在使用GsonFormat时要确保我们输入的Json串信息是正确的,否则生成的Bean类就是错误的,所以当我们拿到Server端返回的Json信息后,最好和接口文档做个对比(如果有接口文档),如果不一致提前修改,确保我们的Json信息是正确的。

好了,就到这里吧,不想手写Bean类的小伙伴们赶紧用起来吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息