zoukankan      html  css  js  c++  java
  • 纯绿色 jsonUtil工具

    1. package com.daditech.common.util;
    2. import java.lang.reflect.Field;
    3. import java.lang.reflect.Method;
    4. import java.math.BigDecimal;
    5. import java.math.BigInteger;
    6. import java.sql.Date;
    7. import java.sql.Timestamp;
    8. import java.text.SimpleDateFormat;
    9. import java.util.HashMap;
    10. import java.util.Iterator;
    11. import java.util.List;
    12. import java.util.Map;
    13. import org.apache.commons.lang.StringUtils;
    14. /**
    15. *
    16. * 项目名称:drp <br/>
    17. * 类名称:JsonUtil <br/>
    18. * 类描述: <br/>
    19. * 创建人:ki_tom <br/>
    20. * 创建时间:2013-8-12 下午2:11:24 <br/>
    21. * 修改人:ao.ouyang <br/>
    22. * 修改时间:2014年10月1日11:25:24 <br/>
    23. * @version 1.2
    24. *
    25. */
    26. public class JsonUtil {
    27. public static final String SEP1 = ":";
    28. public static final String SEP11 = "=";
    29. public static final String SEP2 = ",";
    30. public static boolean isRemoveZeroList;
    31. public static String TimestampPattern = "yyyy-MM-dd HH:mm:ss";
    32. public static String DatePattern = "yyyy-MM-dd HH:mm:ss";
    33. public static boolean isCamelSpelling = true;
    34. /**
    35. *
    36. * @Function string转化为map<String,String> <br/>
    37. * @param mapStr <br/>
    38. * @require <br/>
    39. * @return Map<String,String> <br/>
    40. * @Exception <br/>
    41. * @since  version 1.0 <br/>
    42. * 创建人:ki_tom <br/>
    43. * 创建时间:2013-8-12 下午2:11:39 <br/>
    44. * 修改人:ao.ouyang <br/>
    45. * 修改时间:2014年10月1日11:27:00 <br/>
    46. */
    47. public static Map<String,String> strToMap(String mapStr) {
    48. if (StringUtils.isEmpty(mapStr)) {
    49. return null;
    50. }
    51. mapStr = mapStr.substring(1,mapStr.length()-1).replaceAll("'", "").replace(""", "");
    52. Map<String,String> map = new HashMap<String,String>();
    53. // 转换为数组
    54. String[] text = mapStr.split("\" + SEP2);
    55. for (String str : text) {
    56. // 转换key与value的数组
    57. String[] keyText = str.split(str.indexOf(SEP1)!=-1?SEP1:SEP11);
    58. if (keyText.length <= 1) {
    59. continue;
    60. }
    61. String key = keyText[0]; // key
    62. String value = keyText[1]; // value
    63. map.put(key.trim(), value.trim());
    64. }
    65. return map;
    66. }
    67. public static String toJson(Object obj)
    68. {
    69. if(null!=obj){
    70. if(obj instanceof List){
    71. return toJson((List)obj);
    72. }else if(obj instanceof Object[]){
    73. return toJson((Object[])obj);
    74. }else{
    75. toJson(getAttributes(obj));
    76. }
    77. }
    78. return null;
    79. }
    80. public static String toJson(Map map)
    81. {
    82. String result = "";
    83. map = removeListAttr(map);
    84. for(Iterator it = map.keySet().iterator(); it.hasNext();)
    85. {
    86. String name = (String)it.next();
    87. Object value = map.get(name);
    88. if(value == null)
    89. result = (new StringBuilder()).append(result).append(""").append(name).append("":null,").toString();
    90. else
    91. if(value instanceof Boolean)
    92. {
    93. Boolean b = (Boolean)value;
    94. result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(b.toString()).append(",").toString();
    95. } else
    96. if((value instanceof Integer) || (value instanceof Long) || (value instanceof Float) || (value instanceof Double) || (value instanceof Short) || (value instanceof BigInteger) || (value instanceof BigDecimal))
    97. result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(value.toString()).append(",").toString();
    98. else
    99. if(value instanceof String)
    100. {
    101. String v = (String)value;
    102. v = v.replaceAll("\\", "\\\\");
    103. v = v.replaceAll(" ", "\\n");
    104. v = v.replaceAll(" ", "\\r");
    105. v = v.replaceAll(""", "\\"");
    106. v = v.replaceAll("'", "\\'");
    107. result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(""").append(v).append("",").toString();
    108. } else
    109. if(value instanceof Date || value instanceof java.util.Date)
    110. {
    111. SimpleDateFormat df = new SimpleDateFormat(DatePattern);
    112. String s = df.format(value);
    113. result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(""").append(s).append("",").toString();
    114. } else
    115. if(value instanceof Timestamp)
    116. {
    117. SimpleDateFormat df = new SimpleDateFormat(TimestampPattern);
    118. Timestamp v = (Timestamp)value;
    119. String s = df.format(new java.util.Date(v.getTime()));
    120. result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(""").append(s).append("",").toString();
    121. } else
    122. if(value instanceof List)
    123. {
    124. String v = toJson((List)value);
    125. result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(v).append(",").toString();
    126. } else
    127. if(value instanceof Map)
    128. {
    129. Map attr = removeListAttr((Map)value);
    130. result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(toJson(attr)).append(",").toString();
    131. } else
    132. if(!value.getClass().getName().startsWith("java"))
    133. {
    134. Map attr = getAttributes(value);
    135. attr = removeListAttr(attr);
    136. result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(toJson(attr)).append(",").toString();
    137. } else
    138. {
    139. result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(""").append(value.toString()).append("",").toString();
    140. }
    141. }
    142. if(result.length() == 0)
    143. return "{}";
    144. else
    145. return (new StringBuilder()).append("{").append(result.substring(0, result.length() - 1)).append("}").toString();
    146. }
    147. public static String toJson(Object aa[])
    148. {
    149. if(aa.length == 0)
    150. return "[]";
    151. String result = "";
    152. Object arr$[] = aa;
    153. int len$ = arr$.length;
    154. for(int i$ = 0; i$ < len$; i$++)
    155. {
    156. Object obj = arr$[i$];
    157. if(obj==null){
    158. result = (new StringBuilder()).append(result).append("null").append(",").toString();
    159. continue;
    160. }
    161. else if(obj instanceof Map)
    162. {
    163. Map map = (Map)obj;
    164. map = removeListAttr(map);
    165. result = (new StringBuilder()).append(result).append(toJson(map)).append(",").toString();
    166. continue;
    167. }
    168. else if(obj instanceof List)
    169. {
    170. List arr = (List)obj;
    171. result = (new StringBuilder()).append(result).append(toJson(arr)).append(",").toString();
    172. continue;
    173. }
    174. else if (obj instanceof Object[]) {
    175. Object[] arr = (Object[])obj;
    176. result = (new StringBuilder()).append(result).append(toJson(arr)).append(",").toString();
    177. continue;
    178. }
    179. else if(obj instanceof String)
    180. {
    181. result = (new StringBuilder()).append(result).append(""").append(String.valueOf(obj)).append("",").toString();
    182. continue;
    183. }
    184. else {
    185. Map attr = getAttributes(obj);
    186. attr = removeListAttr(attr);
    187. result = (new StringBuilder()).append(result).append(toJson(attr)).append(",").toString();
    188. }
    189. }
    190. return (new StringBuilder()).append("[").append(result.substring(0, result.length() - 1)).append("]").toString();
    191. }
    192. public static String toJson(List ll)
    193. {
    194. return ll==null? "[]":toJson(ll.toArray());
    195. }
    196. public static Map getAttributes(Object obj)
    197. {
    198. Class c = obj.getClass();
    199. try
    200. {
    201. Method method = c.getMethod("isProxy", new Class[0]);
    202. Boolean result = (Boolean)method.invoke(obj, new Object[0]);
    203. if(result.booleanValue())
    204. c = c.getSuperclass();
    205. }
    206. catch(Exception e) { }
    207. Map attr = new HashMap();
    208. Field arr$[] = c.getFields();
    209. int len$ = arr$.length;
    210. for(int i$ = 0; i$ < len$; i$++)
    211. {
    212. Field f = arr$[i$];
    213. try
    214. {
    215. Object value = f.get(obj);
    216. attr.put(f.getName(), value);
    217. }
    218. catch(Exception e) { }
    219. }
    220. Method [] arr$2 = c.getDeclaredMethods();
    221. len$ = arr$2.length;
    222. for(int i$ = 0; i$ < len$; i$++)
    223. {
    224. Method m = arr$2[i$];
    225. String mname = m.getName();
    226. if(mname.equals("getClass"))
    227. continue;
    228. String pname;
    229. if(mname.startsWith("get"))
    230. {
    231. pname = mname.substring(3);
    232. if(pname.length() == 1)
    233. pname = isCamelSpelling?
    234. pname.toLowerCase()
    235. :pname;
    236. else
    237. pname = isCamelSpelling?
    238. (new StringBuilder())
    239. .append(pname.substring(0, 1).toLowerCase())
    240. .append(pname.substring(1))
    241. .toString()
    242. :pname;
    243. try
    244. {
    245. Object value = m.invoke(obj, new Object[0]);
    246. attr.put(pname, value);
    247. }
    248. catch(Exception e) { }
    249. continue;
    250. }
    251. if(!mname.startsWith("is"))
    252. continue;
    253. pname = mname.substring(2);
    254. if(pname.length() == 1)
    255. pname = pname.toLowerCase();
    256. else
    257. pname = (new StringBuilder()).append(pname.substring(0, 1).toLowerCase()).append(pname.substring(1)).toString();
    258. try
    259. {
    260. Object value = m.invoke(obj, new Object[0]);
    261. attr.put(pname, value);
    262. }
    263. catch(Exception e) { }
    264. }
    265. return attr;
    266. }
    267. private static Map removeListAttr(Map map)
    268. {
    269. Map newMap = new HashMap();
    270. Iterator i$ = map.entrySet().iterator();
    271. do
    272. {
    273. if(!i$.hasNext())
    274. break;
    275. Object enObj = i$.next();
    276. java.util.Map.Entry en = (java.util.Map.Entry)enObj;
    277. if(isRemoveZeroList&&en.getValue() instanceof List &&((List)en.getValue()).size()==0)
    278. continue;
    279. else
    280. newMap.put((String)en.getKey(), en.getValue());
    281. } while(true);
    282. return newMap;
    283. }
    284. }

  • 相关阅读:
    PHP 小方法之 算生日
    PHP 小方法之 随机生成几位字符串
    PHP 小方法之 过滤参数
    PHP 小方法之 计算两个时间戳之间相差的日时分秒
    PHP 小方法之 仿百度蜘蛛采集
    PHP 小方法之 显示 今天 昨天 上周 上月 近三月 的时间
    PHP保留两位小数的几种方法
    mysql 常用命令(一)
    PHP数据库页面增删查
    PHP数据库登陆注册简单做法
  • 原文地址:https://www.cnblogs.com/signheart/p/6595577.html
Copyright © 2011-2022 走看看