枚举转化为JSON字符串
来源:互联网 发布:mac 中文字幕乱码 编辑:程序博客网 时间:2024/05/10 14:55
近期在项目上与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(); }
0 0
- 枚举转化为JSON字符串
- 转化为json字符串
- json转化为字符串
- json数组转化为字符串
- 字典转化为 json 字符串
- json字符串转化为对象
- JSON字符串转化为字典
- 【javascript】Json字符串转化为Json数据
- 将json字符串 转化为 json
- JOSN对象转化为字符串,字符串转化为JSON对象
- 把枚举转化为相应的字符串
- 将枚举类型转化为字符串
- 把字符串转化为json对象
- 【common】JavaScript ----- 对象转化为json字符串
- 如何将字符串转化为json格式
- js关联数组转化为json字符串
- iOS json字符串转化为NSArray(解析)
- Python处理json字符串转化为字典
- RecycleView一页只显示一个列表项
- 互斥锁和条件变量(1)
- (转)今天的人工智能比肩工业革命毫不夸张
- 迪杰斯特拉算法(可打印最短路径)
- strchr函数的使用
- 枚举转化为JSON字符串
- POJ
- 查看安装的centos的版本信息
- HDU 1484 Basic wall maze
- 如何优化js代码
- 前端框架Vue(9)——百度地图使用
- iOS之UITableView(二)系统自带的刷新UIrefreshControl
- BZOJ4832 [Lydsy2017年4月月赛]抵制克苏恩
- HTML标签锚标签