首页
技术知识库
Task工作计划
网站简介
DON框架
后台管理
文章分类
JAVA
框架知识
操作系统
容器相关
数据库层
优化技术
界面编程
网络编程
开发工具
GO语言
其他
读书随笔
观影随笔
每日随笔
APP
自实现JAVA对象转为json字符串
所属分类
:[JAVA] |
创建时间
:2014-11-26 |
文章属性
:原创 |
文章来源
:http://windfly.cn |
作者
:windfly
##功能 >自定义按需实现由java对象转换成json字符串 ##自定义 >1. 只要有get或is方法即视为可输出,如getName(),则输出 "name":xxx , isAdmin(),则输出 "admin":true >1. 返回值为数字类型的,无双引号,返回值为null的不输出,返回值为日期类型的则输出格式为 2014-11-11 11:11:11 >1. 父类有get方法的也会被输出 ##实现代码 ``` public static String toJson(Object obj) { if (obj == null) { return null; } if (obj instanceof String) { return "\"" + obj.toString() + "\""; } if (obj instanceof Map) { @SuppressWarnings({ "rawtypes", "unchecked" }) Set<Map.Entry> entrySet = ((Map) obj).entrySet(); StringBuilder sb = new StringBuilder("{"); for (@SuppressWarnings("rawtypes") Map.Entry entry : entrySet) { sb.append("\"").append(entry.getKey()).append("\"").append(":") .append(toJson(entry.getValue())).append(","); } if (sb.length() > 0) { sb.setLength(sb.length() - 1); } sb.append("}"); return sb.toString(); } if (obj instanceof Number || obj instanceof Boolean) { return String.valueOf(obj); } if (obj instanceof Collection) { StringBuilder sb = new StringBuilder("["); for (Object eo : (Collection<?>) obj) { sb.append(toJson(eo)).append(","); } if (sb.length() > 0) { sb.setLength(sb.length() - 1); } sb.append("]"); return sb.toString(); } if (obj instanceof Date || obj instanceof java.sql.Date || obj instanceof Calendar) { return "\"" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(obj) + "\""; } // object Class<? extends Object> cls = obj.getClass(); if (cls.isPrimitive()) { return cls.toString(); } if (cls.isAnnotation()) { return ""; } if (cls.isArray()) { StringBuilder sb = new StringBuilder("["); int length = Array.getLength(obj); for (int i = 0; i < length; i++) { sb.append(toJson(Array.get(obj, i))).append(","); } if (sb.length() > 0) { sb.setLength(sb.length() - 1); } sb.append("]"); return sb.toString(); } if (cls.isEnum()) { return ""; } if (cls.isInterface()) { return ""; } if (cls.isSynthetic()) { return ""; } StringBuilder sb = new StringBuilder(); sb.append("{"); BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.class); PropertyDescriptor[] propertyDescriptors = beanInfo .getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { Method readMethod = propertyDescriptor.getReadMethod(); if (readMethod == null) { continue; } Object value = null; try { value = readMethod.invoke(obj); } catch (IllegalAccessException e) { logger.log(Level.FINE, e.getMessage(), e); } catch (IllegalArgumentException e) { logger.log(Level.FINE, e.getMessage(), e); } catch (InvocationTargetException e) { logger.log(Level.FINE, e.getMessage(), e); } if (value == null) { continue; } sb.append("\"").append(propertyDescriptor.getName()) .append("\"").append(":"); sb.append(toJson(value)); sb.append(","); } } catch (IntrospectionException e) { throw new RuntimeException(e); } if (sb.length() > 1) { sb.setLength(sb.length() - 1); } sb.append("}"); return sb.toString(); } ```
返回