在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類的小夥伴們趕緊用起來吧!