iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >手把手教学windows下Android Studio使用GsonFormatPlus插件
  • 852
分享到

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

windowsandroidstudiojavajson 2023-09-08 14:09:27 852人浏览 安东尼
摘要

使用api获取数据时经常需要用把JSON转成Java Class,虽然有网页版的工具:在线JSON字符串转Java实体类(JavaBean、Entity)-BeJSON.com,但是我用不明白,这工具会生成三个文件,一个个复制有点麻烦,所以

使用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 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 getDaily() {        return daily;    }    public void setDaily(List daily) {        this.daily = daily;    }    public ReferBean getRefer() {        return refer;    }    public void setRefer(ReferBean refer) {        this.refer = refer;    }    public static class ReferBean {        private List sources;        private List license;        public List getSources() {            return sources;        }        public void setSources(List sources) {            this.sources = sources;        }        public List getLicense() {            return license;        }        public void setLicense(List 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数据时。

来源地址:https://blog.csdn.net/qq_54120557/article/details/129616363

--结束END--

本文标题: 手把手教学windows下Android Studio使用GsonFormatPlus插件

本文链接: https://www.lsjlt.com/news/400035.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作