import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.math.BigDecimal;import java.math.BigInteger;import java.util.List;import java.util.Map;import java.util.Set;/** * 任意对象转化为JSON * * @author ヅ虫虫ゎ * */public class Json { /** * * @param obj * 任意对象 * @return */ public static String objectToJson(Object obj) { System.out.println("objectToJson"); System.out.println("==========="); StringBuilder json = new StringBuilder(); if (obj == null) { json.append("\"\""); } else if (obj instanceof String || obj instanceof Integer || obj instanceof Float || obj instanceof Boolean || obj instanceof Short || obj instanceof Double || obj instanceof Long || obj instanceof BigDecimal || obj instanceof BigInteger || obj instanceof Byte) { json.append("\"").append(stringToJson(obj.toString())).append("\""); } else if (obj instanceof Object[]) { json.append(arrayToJson((Object[]) obj)); } else if (obj instanceof List) { json.append(listToJson((List) obj)); } else if (obj instanceof Map) { json.append(mapToJson((Map) obj)); } else if (obj instanceof Set) { json.append(setToJson((Set) obj)); } else { json.append(beanToJson(obj)); } System.out.println(json.toString()); return json.toString(); } /** * * @param str * 字符串对象 * @return */ public static String stringToJson(String str) { System.out.println("stringToJson"); System.out.println("=========="); if (str == null) { return ""; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); switch (ch) { case '"': sb.append("\\\""); break; case '\\': sb.append("\\\\"); break; case '\b': sb.append("\\b"); break; case '\f': sb.append("\\f"); break; case '\n': sb.append("\\n"); break; case '\r': sb.append("\\r"); break; case '\t': sb.append("\\t"); break; case '/': sb.append("\\/"); break; default: if (ch >= '\u0000' && ch <= '\u001F') { String ss = Integer.toHexString(ch); sb.append("\\u"); for (int k = 0; k < 4 - ss.length(); k++) { sb.append('0'); } sb.append(ss.toUpperCase()); } else { sb.append(ch); } } } System.out.println(sb.toString()); return sb.toString(); } /** * * @param array * 对象数组 * @return */ public static String arrayToJson(Object[] array) { System.out.println("arrayToJson"); System.out.println("=========="); StringBuilder json = new StringBuilder(); json.append("["); if (array != null && array.length > 0) { for (Object obj : array) { json.append(objectToJson(obj)); json.append(","); } json.setCharAt(json.length() - 1, ']'); } else { json.append("]"); } System.out.println(json.toString()); return json.toString(); } /** * * @param list * List对象 * @return */ public static String listToJson(List list) { System.out.println("listToJson"); System.out.println("=========="); StringBuilder json = new StringBuilder(); json.append("["); if (list != null && list.size() > 0) { for (Object obj : list) { json.append(objectToJson(obj)); json.append(","); } json.setCharAt(json.length() - 1, ']'); } else { json.append("]"); } System.out.println(json.toString()); return json.toString(); } /** * * @param map * Map对象 * @return */ public static String mapToJson(Map map) { System.out.println("mapToJson"); System.out.println("========="); StringBuilder json = new StringBuilder(); json.append("{"); if (map != null && map.size() > 0) { for (Object key : map.keySet()) { json.append(objectToJson(key)); json.append(":"); json.append(objectToJson(map.get(key))); json.append(","); } json.setCharAt(json.length() - 1, '}'); } else { json.append("}"); } System.out.println(json.toString()); return json.toString(); } /** * * @param set * Set对象 * @return */ public static String setToJson(Set set) { System.out.println("setToJson"); System.out.println("=========="); StringBuilder json = new StringBuilder(); json.append("["); if (set != null && set.size() > 0) { for (Object obj : set) { json.append(objectToJson(obj)); json.append(","); } json.setCharAt(json.length() - 1, ']'); } else { json.append("]"); } System.out.println(json.toString()); return json.toString(); } /** * * @param bean * Bean对象 * @return */ public static String beanToJson(Object bean) { System.out.println("beanToJson"); System.out.println("=========="); StringBuilder json = new StringBuilder(); json.append("{"); PropertyDescriptor[] props = null; try { props = Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors(); } catch (IntrospectionException e) { } if (props != null) { for (int i = 0; i < props.length; i++) { try { String name = objectToJson(props[i].getName()); String value = objectToJson(props[i].getReadMethod().invoke(bean)); json.append(name); json.append(":"); json.append(value); json.append(","); } catch (Exception e) { } } json.setCharAt(json.length() - 1, '}'); } else { json.append("}"); } System.out.println(json.toString()); return json.toString(); }}
网友评论