插件地址: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
{
{
[
,
}
,
}
}
}

 

自定义个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 解析成实体

生成如下:

  1 package com.yuanding.entity;
  2  
  3 import java.util.List;
  4  
  5 /**
  6 * Created by diyvc on 2017/3/13.
  7 */
  8 public class TestClass {
  9  
 10  
 11     /**
 12      * animals : {"dog":[{"name":"Rufus","breed":"labrador","count":1,"twoFeet":false},{"name":"Marty","breed":"whippet","count":1,"twoFeet":false}],"cat":{"name":"Matilda"}}
 13      */
 14  
 15     private AnimalsBean animals;
 16  
 17     public AnimalsBean getAnimals() {
 18         return animals;
 19     }
 20  
 21     public void setAnimals(AnimalsBean animals) {
 22         this.animals = animals;
 23     }
 24  
 25     public static class AnimalsBean {
 26         /**
 27          * dog : [{"name":"Rufus","breed":"labrador","count":1,"twoFeet":false},{"name":"Marty","breed":"whippet","count":1,"twoFeet":false}]
 28          * cat : {"name":"Matilda"}
 29          */
 30  
 31         private CatBean cat;
 32         private List<DogBean> dog;
 33  
 34         public CatBean getCat() {
 35             return cat;
 36         }
 37  
 38         public void setCat(CatBean cat) {
 39             this.cat = cat;
 40         }
 41  
 42         public List<DogBean> getDog() {
 43             return dog;
 44         }
 45  
 46         public void setDog(List<DogBean> dog) {
 47             this.dog = dog;
 48         }
 49  
 50         public static class CatBean {
 51             /**
 52              * name : Matilda
 53              */
 54  
 55             private String name;
 56  
 57             public String getName() {
 58                 return name;
 59             }
 60  
 61             public void setName(String name) {
 62                 this.name = name;
 63             }
 64         }
 65  
 66         public static class DogBean {
 67             /**
 68              * name : Rufus
 69              * breed : labrador
 70              * count : 1
 71              * twoFeet : false
 72              */
 73  
 74             private String name;
 75             private String breed;
 76             private int count;
 77             private boolean twoFeet;
 78  
 79             public String getName() {
 80                 return name;
 81             }
 82  
 83             public void setName(String name) {
 84                 this.name = name;
 85             }
 86  
 87             public String getBreed() {
 88                 return breed;
 89             }
 90  
 91             public void setBreed(String breed) {
 92                 this.breed = breed;
 93             }
 94  
 95             public int getCount() {
 96                 return count;
 97             }
 98  
 99             public void setCount(int count) {
100                 this.count = count;
101             }
102  
103             public boolean isTwoFeet() {
104                 return twoFeet;
105             }
106  
107             public void setTwoFeet(boolean twoFeet) {
108                 this.twoFeet = twoFeet;
109             }
110         }
111     }
112 }

 

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

相关文章: