手把手教学windows下Android Studio使用GsonFormatPlus插件

使用API获取数据时经常需要用把JSON转成Java Class,虽然有网页版的工具:在线JSON字符串转Java实体类(JavaBean、Entity)-BeJSON.com,但是我用不明白,这工具会生成三个文件,一个个复制有点麻烦,所以还是用GsonFormat工具吧,目前csdn上搜好几个要么是MacOS的,本人新手都找不到怎么打开Plugins,要么windows下教你安装但是生成出来又不对,所以自己写一篇文章记录一下怎么用吧。

我的AS版本:2022.1.1.21电鳗版,skr~

首先介绍一下GsonFormatPlus插件:

gsonformatplus是一个IntelliJ IDEA插件,用于将JSON格式的字符串转换为Java对象。使用该插件可以快速生成Java类,省去手动编写Java类的繁琐过程。具体使用方法如下:

  1. 安装插件:在IntelliJ IDEA中打开插件市场,搜索gsonformatplus插件并安装。

Android Studio反正也是IDEA改的,所以这里也一样,点击File-Settings-Plugins

搜索框里输入gson,网络慢的可能得稍等一下,然后找到GsonFormatPlus,点击Install。

注意!!!这里点击Install安装后要检查一下是否可以Apply,要点击Apply之后才可以使用,如果没点Apply即使重启了也可能无法使用,需要重新进来安装再应用!!!

  1. 使用方法:右键-Generate-GsonFormatPlus,即可弹出窗口

安装后随便新建一个Class,在class的声明代码中右键就可以弹出这个窗口,没有的话请检查上一步

  1. 根据个人需求设置Setting

虽然这时候就可以把JSON文本复制到左边,然后点击OK来生成代码了!但不要急,现在的设置生成的不是GsonFormat(没有plus)的版本,默认生成的是使用序列化的名字和lombok的,如果要用的话需要另外配置,而我是传统派,可以参考我下图的设置

主要是关闭序列化名称和lombok,并选择转换库为Gson,ok!可以复制粘贴生成了!

  1. 举个例子:使用和风天气提供的天气信息生成Class文件

首先复制通过API,从网页获取天气信息,并把信息复制进来

这时他就会自动生成一长段的代码:

package com.animee.forecast.hefeng;

import java.util.List;

;

public class HFTempBean {

    private String code;
    private String updateTime;
    private String fxLink;
    private List<DailyBean> daily;
    private ReferBean refer;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public String getFxLink() {
        return fxLink;
    }

    public void setFxLink(String fxLink) {
        this.fxLink = fxLink;
    }

    public List<DailyBean> getDaily() {
        return daily;
    }

    public void setDaily(List<DailyBean> daily) {
        this.daily = daily;
    }

    public ReferBean getRefer() {
        return refer;
    }

    public void setRefer(ReferBean refer) {
        this.refer = refer;
    }

    public static class ReferBean {
        private List<String> sources;
        private List<String> license;

        public List<String> getSources() {
            return sources;
        }

        public void setSources(List<String> sources) {
            this.sources = sources;
        }

        public List<String> getLicense() {
            return license;
        }

        public void setLicense(List<String> license) {
            this.license = license;
        }
    }

    public static class DailyBean {
        private String fxDate;
        private String sunrise;
        private String sunset;
        private String moonrise;
        private String moonset;
        private String moonPhase;
        private String moonPhaseIcon;
        private String tempMax;
        private String tempMin;
        private String iconDay;
        private String textDay;
        private String iconNight;
        private String textNight;
        private String wind360Day;
        private String windDirDay;
        private String windScaleDay;
        private String windSpeedDay;
        private String wind360Night;
        private String windDirNight;
        private String windScaleNight;
        private String windSpeedNight;
        private String humidity;
        private String precip;
        private String pressure;
        private String vis;
        private String cloud;
        private String uvIndex;

        public String getFxDate() {
            return fxDate;
        }

        public void setFxDate(String fxDate) {
            this.fxDate = fxDate;
        }

        public String getSunrise() {
            return sunrise;
        }

        public void setSunrise(String sunrise) {
            this.sunrise = sunrise;
        }

        public String getSunset() {
            return sunset;
        }

        public void setSunset(String sunset) {
            this.sunset = sunset;
        }

        public String getMoonrise() {
            return moonrise;
        }

        public void setMoonrise(String moonrise) {
            this.moonrise = moonrise;
        }

        public String getMoonset() {
            return moonset;
        }

        public void setMoonset(String moonset) {
            this.moonset = moonset;
        }

        public String getMoonPhase() {
            return moonPhase;
        }

        public void setMoonPhase(String moonPhase) {
            this.moonPhase = moonPhase;
        }

        public String getMoonPhaseIcon() {
            return moonPhaseIcon;
        }

        public void setMoonPhaseIcon(String moonPhaseIcon) {
            this.moonPhaseIcon = moonPhaseIcon;
        }

        public String getTempMax() {
            return tempMax;
        }

        public void setTempMax(String tempMax) {
            this.tempMax = tempMax;
        }

        public String getTempMin() {
            return tempMin;
        }

        public void setTempMin(String tempMin) {
            this.tempMin = tempMin;
        }

        public String getIconDay() {
            return iconDay;
        }

        public void setIconDay(String iconDay) {
            this.iconDay = iconDay;
        }

        public String getTextDay() {
            return textDay;
        }

        public void setTextDay(String textDay) {
            this.textDay = textDay;
        }

        public String getIconNight() {
            return iconNight;
        }

        public void setIconNight(String iconNight) {
            this.iconNight = iconNight;
        }

        public String getTextNight() {
            return textNight;
        }

        public void setTextNight(String textNight) {
            this.textNight = textNight;
        }

        public String getWind360Day() {
            return wind360Day;
        }

        public void setWind360Day(String wind360Day) {
            this.wind360Day = wind360Day;
        }

        public String getWindDirDay() {
            return windDirDay;
        }

        public void setWindDirDay(String windDirDay) {
            this.windDirDay = windDirDay;
        }

        public String getWindScaleDay() {
            return windScaleDay;
        }

        public void setWindScaleDay(String windScaleDay) {
            this.windScaleDay = windScaleDay;
        }

        public String getWindSpeedDay() {
            return windSpeedDay;
        }

        public void setWindSpeedDay(String windSpeedDay) {
            this.windSpeedDay = windSpeedDay;
        }

        public String getWind360Night() {
            return wind360Night;
        }

        public void setWind360Night(String wind360Night) {
            this.wind360Night = wind360Night;
        }

        public String getWindDirNight() {
            return windDirNight;
        }

        public void setWindDirNight(String windDirNight) {
            this.windDirNight = windDirNight;
        }

        public String getWindScaleNight() {
            return windScaleNight;
        }

        public void setWindScaleNight(String windScaleNight) {
            this.windScaleNight = windScaleNight;
        }

        public String getWindSpeedNight() {
            return windSpeedNight;
        }

        public void setWindSpeedNight(String windSpeedNight) {
            this.windSpeedNight = windSpeedNight;
        }

        public String getHumidity() {
            return humidity;
        }

        public void setHumidity(String humidity) {
            this.humidity = humidity;
        }

        public String getPrecip() {
            return precip;
        }

        public void setPrecip(String precip) {
            this.precip = precip;
        }

        public String getPressure() {
            return pressure;
        }

        public void setPressure(String pressure) {
            this.pressure = pressure;
        }

        public String getVis() {
            return vis;
        }

        public void setVis(String vis) {
            this.vis = vis;
        }

        public String getCloud() {
            return cloud;
        }

        public void setCloud(String cloud) {
            this.cloud = cloud;
        }

        public String getUvIndex() {
            return uvIndex;
        }

        public void setUvIndex(String uvIndex) {
            this.uvIndex = uvIndex;
        }
    }
}

这样就ok了!虽然又臭又长是吧哈哈。如果你觉得这样代码太长,可读性很差,可以使用lombok来通过注释减少代码量,可以参考下面两个文章:

(14条消息) Lombok使用详解_鞠骞的博客的博客-CSDN博客
(13条消息) AndroidStudio插件 GsonFormatPlus使用_3Blue1Red的博客-CSDN博客

总之,GsonFormatPlus插件是一个非常方便的工具,可以大大提高开发效率,特别是在处理JSON数据时。

猜你喜欢

转载自blog.csdn.net/qq_54120557/article/details/129616363