IntelliJ IDEA 通过GsonFormat插件将JSONObject格式的String 解析成实体

时间:2023-03-09 19:56:40
IntelliJ IDEA 通过GsonFormat插件将JSONObject格式的String 解析成实体

GsonFormat插件主要用于使用Gson库将JSONObject格式的String 解析成实体,该插件可以加快开发进度,使用非常方便,效率高。

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

这个教程主要是学习IntelliJ IDEA 如何通过GsonFormat插件将JSONObject格式的String 解析成实体。

一般来说

IDEA的插件安装非常简单,对于很多插件来说,只要你知道插件的名字就可以在IDEA里面直接安装。
File->Settings->Plugins—>查找所需插件—>Install
或者
File->Settings->Plugins—>Install plug from disk —>选择下载好的插件安装

一般插件安装之后重启IDEA即可生效。

下面详细安装图文:

先到setting里面,然后通过搜索Plugins

IntelliJ IDEA 通过GsonFormat插件将JSONObject格式的String 解析成实体

然后插件栏搜索GsonFormat。

IntelliJ IDEA 通过GsonFormat插件将JSONObject格式的String 解析成实体

安装即可。

安装完,需要重启一下idea。

IntelliJ IDEA 通过GsonFormat插件将JSONObject格式的String 解析成实体

通过json

1
2
3
4
5
6
7
8
9
{
"animals":{
"dog":[
{"name":"Rufus","breed":"labrador","count":1,"twoFeet":false},
{"name":"Marty","breed":"whippet","count":1,"twoFeet":false}
],
"cat":{"name":"Matilda"}
}
}

自定义个javaBean(无任何内容,就一个空的类)

复制你要解析的json

然后alt+insert弹出如下界面 或者使用快捷键 alt+s

通过快捷键调出该插件

IntelliJ IDEA 通过GsonFormat插件将JSONObject格式的String 解析成实体

格式化json

IntelliJ IDEA 通过GsonFormat插件将JSONObject格式的String 解析成实体

可以设置

IntelliJ IDEA 通过GsonFormat插件将JSONObject格式的String 解析成实体

ok即可生成实体类

配置生成名

IntelliJ IDEA 通过GsonFormat插件将JSONObject格式的String 解析成实体

生成如下:

 package com.yuanding.entity;

 import java.util.List;

 /**
* Created by diyvc on 2017/3/13.
*/
public class TestClass { /**
* animals : {"dog":[{"name":"Rufus","breed":"labrador","count":1,"twoFeet":false},{"name":"Marty","breed":"whippet","count":1,"twoFeet":false}],"cat":{"name":"Matilda"}}
*/ private AnimalsBean animals; public AnimalsBean getAnimals() {
return animals;
} public void setAnimals(AnimalsBean animals) {
this.animals = animals;
} public static class AnimalsBean {
/**
* dog : [{"name":"Rufus","breed":"labrador","count":1,"twoFeet":false},{"name":"Marty","breed":"whippet","count":1,"twoFeet":false}]
* cat : {"name":"Matilda"}
*/ private CatBean cat;
private List<DogBean> dog; public CatBean getCat() {
return cat;
} public void setCat(CatBean cat) {
this.cat = cat;
} public List<DogBean> getDog() {
return dog;
} public void setDog(List<DogBean> dog) {
this.dog = dog;
} public static class CatBean {
/**
* name : Matilda
*/ private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
} public static class DogBean {
/**
* name : Rufus
* breed : labrador
* count : 1
* twoFeet : false
*/ private String name;
private String breed;
private int count;
private boolean twoFeet; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getBreed() {
return breed;
} public void setBreed(String breed) {
this.breed = breed;
} public int getCount() {
return count;
} public void setCount(int count) {
this.count = count;
} public boolean isTwoFeet() {
return twoFeet;
} public void setTwoFeet(boolean twoFeet) {
this.twoFeet = twoFeet;
}
}
}
}

需要好看的话,自己配置一下。