枚举转化为JSON字符串

近期在项目上与ERP做接口服务时,需要大量用到类别码,JAVA使用枚举值来存储类别码.

下面分享下将枚举值反射成JSON串


    /**
     * 通过反射机制,将枚举值转化为json串
     * @param enumValues
     * @return
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static String toJson(Enum[] enumValues) throws IllegalAccessException, InvocationTargetException {
        StringBuffer buffer=new StringBuffer("[");
        boolean obj1st=true;
        for (Object obj : enumValues) {
            if(obj1st){
                obj1st=false;
            }else{
                buffer.append(",");
            }
            buffer.append("{");

            Method[] methods = obj.getClass().getMethods();
            boolean method1st=true;
            for (int i = 0; i < methods.length; i++) {

                Method method = methods[i];
                //获取枚举值的get方法
                if (method.getName().startsWith("get") && method.getParameterTypes().length == 0 && !method.getName().contains("Class")) {
                    //处理逗号
                    if(method1st){
                        method1st=false;
                    }else{
                        buffer.append(",");
                    }
                    //将get方法的get去掉,并且首字母小写
                    String name = method.getName().replace("get","");
                    buffer.append("\"" + name.substring(0, 1).toLowerCase() + name.substring(1) + "\":\"");
                    buffer.append(method.invoke(obj)+"\"");
                }
            }
            buffer.append("}");
        }
        buffer.append("]");
        return buffer.toString();
    }


你可能感兴趣的:(枚举转化为JSON字符串)