In Android development, we cannot avoid dealing with the Server side. After requesting the interface, the Server side generally returns a Json string of information. We get the Json string and convert it into the Bean we need through JsonObject or JsonArray. In the project, we usually use Google’s Gson or Ali’s FastJson to process Json strings, which are converted into beans by reflection. So what we need to do is to write the corresponding Bean class according to the returned Json string. For example, the Json string returned by the Server looks like this:

{
    "Category": [{"categoryId": 1,
            "categoryName": "Drink"."categoryImage": "/upload/yinpin.jpg"
        },
        {
            "categoryId": 2."categoryName": "Food"."categoryImage": "/upload/shiping.jpg"
        },
        {
            "categoryId": 3."categoryName": "Alcohol"."categoryImage": "/upload/jiullei.jpg"}]."recommend": {
        "id": 11."productName": "Uni-president Old altar Beef bag noodles with pickled pepper 110g*24 bags"."filenameSmall": "/upload/ty_ltpj_small.jpg"."productPrice": 48."productCost": 47.5}}Copy the code

We need according to the above Json string to write the corresponding Bean class, see the above fields or a lot, so to write out all the fields are still have some work, and hand write error prone, once the field is not corresponding, may cause the whole parsing process error, this time GsonFormat couldn’t wait to jump out to: Use me! Use me!

GsonFormat is a plugin in Android Studio that can greatly speed up the generation of Bean classes. Let’s take a look at the process of generating beans using GsonFormat.

1. First go to the Plugins interface in Android Studio Settings, as shown below:

2, Search for GsonFormat in the input box and click Browse Repositories… , to the following interface:

To use the plugin, click install and restart AS. In the blank area of the Bean class, select Generate… , as shown below:

4. Enter the Json string returned by the Server in the input box and click OK:

5. Since the Json string in this example produces two inner classes, GsonFormat has already suggested the name of the inner class, so we can continue by clicking OK:

Finally, take a look at the generated result:

public class noodleBean {

    /**
     * Category : [{"categoryId": 1,"categoryName":"Drink"."categoryImage":"/upload/yinpin.jpg"}, {"categoryId": 2."categoryName":"Food"."categoryImage":"/upload/shiping.jpg"}, {"categoryId": 3."categoryName":"Alcohol"."categoryImage":"/upload/jiullei.jpg"}]
     * recommend : {"id": 11."productName":"Uni-president Old altar Beef bag noodles with pickled pepper 110g*24 bags"."filenameSmall":"/upload/ty_ltpj_small.jpg"."productPrice": 48."productCost": 03/03/2015 */ private RecommendBean recommend; private List<CategoryBean> Category; public RecommendBeangetRecommend() {
        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: unified old altar sweet chili pepper beef bag of 110 g * * filenameSmall 24 bag: /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 intgetId() {
            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: beverage * categoryImage: /upload/yinpin.jpg */ private int categoryId; private String categoryName; private String categoryImage; public intgetCategoryId() {
            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; }}}Copy the code

How’s that? It is much faster than writing by hand, and there is no error. However, when using GsonFormat, we need to make sure that the Json string information we input is correct, otherwise the generated Bean class will be wrong. Therefore, when we get the Json information returned by the Server, it is best to compare it with the interface document (if there is an interface document). If inconsistent, modify in advance to make sure our Json information is correct. All right, that’s it. If you don’t want to write beans by hand, use them.