IDEA插件系列(23):GsonFormat插件——JSON字符串转换Java实体类

mac2026-05-21  5

1. 插件介绍

 

2. 安装方式

第一种安装方式是使用IDEA下载插件进行安装

第二种安装方式是使用离线插件进行安装

插件下载地址:https://plugins.jetbrains.com/plugin/7654-gsonformat/

3. 使用方法

现在项目的默认包目录下创建一个类

右键单击

选中GsonFormat,或者使用快捷键打开

打开面板

设置面板如下:

格式化如下:

点击OK后出现如下面板:

很可惜,失败了

报如下错误:

解决如下:

在原有的项目目录下创建一个包名叫bean的包。

再在bean包下创建一个User类

同理,从第一步重新开始重新转换JSON字符串

这一次就创建成功了

内容如下:

package bean; public class User { /** * name : 张三 * age : 15 * sex : 男 * job : {"firstJob":"文学家","secondJob":"化学家","thirdJob":"生物学家"} */ private String name; private int age; private String sex; private JobBean job; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public JobBean getJob() { return job; } public void setJob(JobBean job) { this.job = job; } public static class JobBean { /** * firstJob : 文学家 * secondJob : 化学家 * thirdJob : 生物学家 */ private String firstJob; private String secondJob; private String thirdJob; public String getFirstJob() { return firstJob; } public void setFirstJob(String firstJob) { this.firstJob = firstJob; } public String getSecondJob() { return secondJob; } public void setSecondJob(String secondJob) { this.secondJob = secondJob; } public String getThirdJob() { return thirdJob; } public void setThirdJob(String thirdJob) { this.thirdJob = thirdJob; } } }

注意必须创建一个包,对于JSON字符串创建实体类有要求可以自行设置。

最新回复(0)