枚举通用,终于不用再一个个的写了
附上代码,利用了spring的org.springframework.beans.BeanWrapperImpl对对象的拆解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * @description 枚举类转换为json对象 * @author liukx * @date 2020/6/3 0003 */ public static JSONObject convertEnumToJson(Enum anEnum){ JSONObject json = new JSONObject(); if(anEnum == null){ return json; } json.put("enumName",anEnum.name()); BeanWrapper src = new BeanWrapperImpl(anEnum); PropertyDescriptor[] pds = src.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { String key = pd.getName(); if(EnumSerializer.ignoreList.contains(key)){ continue; } json.put(key,src.getPropertyValue(key)); } return json; } |
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 | package com.liukx; /** * 枚举类样例 **/ public enum Education { /** * 高中及以下 */ HIGHANDBELOW(1,"高中及以下"), /** * 大专 */ COLLEGE(3,"大专"), /** * 大学本科 */ UNIVERSITY(4,"大学本科"), /** * 硕士及以上 */ MASTERANDABOVE(6,"硕士及以上"), /** * 其他 */ OTHERS(9,"其他"); private int id; private String edu; Education(int id, String edu) { this.id = id; this.edu = edu; } public int getId() { return id; } public String getEdu() { return edu; } } |
哈哈,推荐一些我的github,还有一些有意思的小东西
https://github.com/lkx1993/util