这是网上一个大神实现的,具体出处已找不到,在这做个记录,方便以后使用。
1 package com.wb.test; 2 import java.beans.IntrospectionException; 3 import java.beans.Introspector; 4 import java.beans.PropertyDescriptor; 5 import java.math.BigDecimal; 6 import java.math.BigInteger; 7 import java.util.List; 8 import java.util.Map; 9 import java.util.Set; 10 /** 11 * 将其他数据类型转换成json字符串格式 12 */ 13 public class JsonUtil { 14 15 /** 16 * 入口方法 17 * @param obj 18 * @return 19 */ 20 public static String object2json(Object obj) { 21 StringBuilder json = new StringBuilder(); 22 if (obj == null) { 23 json.append(""""); 24 } else if (obj instanceof String || obj instanceof Integer || obj instanceof Float 25 || obj instanceof Boolean || obj instanceof Short || obj instanceof Double 26 || obj instanceof Long || obj instanceof BigDecimal || obj instanceof BigInteger 27 || obj instanceof Byte) { 28 json.append(""").append(string2json(obj.toString())).append("""); 29 } else if (obj instanceof Object[]) { 30 json.append(array2json((Object[]) obj)); 31 } else if (obj instanceof List) { 32 json.append(list2json((List<?>) obj)); 33 } else if (obj instanceof Map) { 34 json.append(map2json((Map<?, ?>) obj)); 35 } else if (obj instanceof Set) { 36 json.append(set2json((Set<?>) obj)); 37 } else { 38 json.append(bean2json(obj)); 39 } 40 return json.toString(); 41 } 42 43 public static String string2json(String s) { 44 if (s == null) 45 return ""; 46 StringBuilder sb = new StringBuilder(); 47 for (int i = 0; i < s.length(); i++) { 48 char ch = s.charAt(i); 49 switch (ch) { 50 case '"': 51 sb.append("\""); 52 break; 53 case '\': 54 sb.append("\\"); 55 break; 56 case '': 57 sb.append("\b"); 58 break; 59 case 'f': 60 sb.append("\f"); 61 break; 62 case ' ': 63 sb.append("\n"); 64 break; 65 case ' ': 66 sb.append("\r"); 67 break; 68 case ' ': 69 sb.append("\t"); 70 break; 71 case '/': 72 sb.append("\/"); 73 break; 74 default: 75 if (ch >= 'u0000' && ch <= 'u001F') { 76 String ss = Integer.toHexString(ch); 77 sb.append("\u"); 78 for (int k = 0; k < 4 - ss.length(); k++) { 79 sb.append('0'); 80 } 81 sb.append(ss.toUpperCase()); 82 } else { 83 sb.append(ch); 84 } 85 } 86 } 87 return sb.toString(); 88 } 89 90 public static String array2json(Object[] array) { 91 StringBuilder json = new StringBuilder(); 92 json.append("["); 93 if (array != null && array.length > 0) { 94 for (Object obj : array) { 95 json.append(object2json(obj)); 96 json.append(","); 97 } 98 json.setCharAt(json.length() - 1, ']'); 99 } else { 100 json.append("]"); 101 } 102 return json.toString(); 103 } 104 105 public static String list2json(List<?> list) { 106 StringBuilder json = new StringBuilder(); 107 json.append("["); 108 if (list != null && list.size() > 0) { 109 for (Object obj : list) { 110 json.append(object2json(obj)); 111 json.append(","); 112 } 113 json.setCharAt(json.length() - 1, ']'); 114 } else { 115 json.append("]"); 116 } 117 return json.toString(); 118 } 119 120 public static String map2json(Map<?, ?> map) { 121 StringBuilder json = new StringBuilder(); 122 json.append("{"); 123 if (map != null && map.size() > 0) { 124 for (Object key : map.keySet()) { 125 json.append(object2json(key)); 126 json.append(":"); 127 json.append(object2json(map.get(key))); 128 json.append(","); 129 } 130 json.setCharAt(json.length() - 1, '}'); 131 } else { 132 json.append("}"); 133 } 134 return json.toString(); 135 } 136 137 public static String set2json(Set<?> set) { 138 StringBuilder json = new StringBuilder(); 139 json.append("["); 140 if (set != null && set.size() > 0) { 141 for (Object obj : set) { 142 json.append(object2json(obj)); 143 json.append(","); 144 } 145 json.setCharAt(json.length() - 1, ']'); 146 } else { 147 json.append("]"); 148 } 149 return json.toString(); 150 } 151 152 public static String bean2json(Object bean) { 153 StringBuilder json = new StringBuilder(); 154 json.append("{"); 155 PropertyDescriptor[] props = null; 156 try { 157 props = Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors(); 158 } catch (IntrospectionException e) {} 159 if (props != null) { 160 for (int i = 0; i < props.length; i++) { 161 try { 162 String name = object2json(props[i].getName()); 163 String value = object2json(props[i].getReadMethod().invoke(bean)); 164 json.append(name); 165 json.append(":"); 166 json.append(value); 167 json.append(","); 168 } catch (Exception e) {} 169 } 170 json.setCharAt(json.length() - 1, '}'); 171 } else { 172 json.append("}"); 173 } 174 return json.toString(); 175 } 176 177 }