GsonFormat插件主要用于使用Gson库将JSONObject格式的String 解析成实体,该插件可以加快开发进度,使用非常方便,效率高。
这个教程主要是学习IntelliJ IDEA 如何通过GsonFormat插件将JSONObject格式的String 解析成实体。
一般来说
IDEA的插件安装非常简单,对于很多插件来说,只要你知道插件的名字就可以在IDEA里面直接安装。
File->Settings->Plugins—>查找所需插件—>Install
或者
File->Settings->Plugins—>Install plug from disk —>选择下载好的插件安装
一般插件安装之后重启IDEA即可生效。
下面详细安装图文:
先到setting里面,然后通过搜索Plugins
然后插件栏搜索GsonFormat。
安装即可。
安装完,需要重启一下idea。
通过json
{
"animals":{
"dog":[
{"name":"Rufus","breed":"labrador","count":1,"twoFeet":false},
{"name":"Marty","breed":"whippet","count":1,"twoFeet":false}
],
"cat":{"name":"Matilda"}
}
}
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
通过快捷键调出该插件
格式化json
可以设置
ok即可生成实体类
配置生成名
生成如下:
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 dog;
public CatBean getCat() {
return cat;
}
public void setCat(CatBean cat) {
this.cat = cat;
}
public List getDog() {
return dog;
}
public void setDog(List 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;
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
packagecom.yuanding.entity;
importjava.util.List;
/**
* Created by diyvc on 2017/3/13.
*/
publicclassTestClass{
/**
* animals : {"dog":[{"name":"Rufus","breed":"labrador","count":1,"twoFeet":false},{"name":"Marty","breed":"whippet","count":1,"twoFeet":false}],"cat":{"name":"Matilda"}}
*/
privateAnimalsBeananimals;
publicAnimalsBeangetAnimals(){
returnanimals;
}
publicvoidsetAnimals(AnimalsBeananimals){
this.animals=animals;
}
publicstaticclassAnimalsBean{
/**
* dog : [{"name":"Rufus","breed":"labrador","count":1,"twoFeet":false},{"name":"Marty","breed":"whippet","count":1,"twoFeet":false}]
* cat : {"name":"Matilda"}
*/
privateCatBeancat;
privateListdog;
publicCatBeangetCat(){
returncat;
}
publicvoidsetCat(CatBeancat){
this.cat=cat;
}
publicListgetDog(){
returndog;
}
publicvoidsetDog(Listdog){
this.dog=dog;
}
publicstaticclassCatBean{
/**
* name : Matilda
*/
privateStringname;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}
publicstaticclassDogBean{
/**
* name : Rufus
* breed : labrador
* count : 1
* twoFeet : false
*/
privateStringname;
privateStringbreed;
privateintcount;
privatebooleantwoFeet;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetBreed(){
returnbreed;
}
publicvoidsetBreed(Stringbreed){
this.breed=breed;
}
publicintgetCount(){
returncount;
}
publicvoidsetCount(intcount){
this.count=count;
}
publicbooleanisTwoFeet(){
returntwoFeet;
}
publicvoidsetTwoFeet(booleantwoFeet){
this.twoFeet=twoFeet;
}
}
}
}
需要好看的话,自己配置一下。
如果您认为本教程质量不错,读后觉得收获很大,预期工资能蹭蹭蹭的往上涨,那么不妨小额赞助我一下,让我有动力继续写出高质量的教程。