1. 程式人生 > >IntelliJ IDEA 通過GsonFormat插件將JSONObject格式的String 解析成實體

IntelliJ IDEA 通過GsonFormat插件將JSONObject格式的String 解析成實體

界面 tel dea 很多 syntax idea urn plain ans

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

技術分享

然後插件欄搜索GsonFormat。

技術分享

安裝即可。

安裝完,需要重啟一下idea。

技術分享

通過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

通過快捷鍵調出該插件

技術分享

格式化json

技術分享

可以設置

技術分享

ok即可生成實體類

配置生成名

技術分享

生成如下:

  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 }

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

IntelliJ IDEA 通過GsonFormat插件將JSONObject格式的String 解析成實體