任意对象转换为JSON格式

  1. //前阵子在网上找到的,源地址就不知道了,这段代码很实用,对大家很有帮助
  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格式 遵循JSON组织公布标准
  12.      * 
  13.      * @date 2008/05/07
  14.      * @version 1.0.0
  15.      */
  16.     public class Json {
  17.         /** Commons Logging instance. */
  18.         private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(Json.class);
  19.         /**
  20.          * @param obj 任意对象
  21.          * @return String
  22.          */
  23.         public static String object2json(Object obj) {
  24.             StringBuilder json = new StringBuilder();
  25.             if (obj == null) {
  26.                 json.append("/"/"");
  27.             } else if (obj instanceof String || obj instanceof Integer || obj instanceof Float || obj instanceof Boolean
  28.                     || obj instanceof Short || obj instanceof Double || obj instanceof Long || obj instanceof BigDecimal
  29.                     || obj instanceof BigInteger || obj instanceof Byte) {
  30.                 json.append("/"").append(string2json(obj.toString())).append("/"");
  31.             } else if (obj instanceof Object[]) {
  32.                 json.append(array2json((Object[]) obj));
  33.             } else if (obj instanceof List) {
  34.                 json.append(list2json((List<?>) obj));
  35.             } else if (obj instanceof Map) {
  36.                 json.append(map2json((Map<?, ?>) obj));
  37.             } else if (obj instanceof Set) {
  38.                 json.append(set2json((Set<?>) obj));
  39.             } else {
  40.                 json.append(bean2json(obj));
  41.             }
  42.             return json.toString();
  43.         }
  44.         /**
  45.          * @param bean bean对象
  46.          * @return String
  47.          */
  48.         public static String bean2json(Object bean) {
  49.             StringBuilder json = new StringBuilder();
  50.             json.append("{");
  51.             PropertyDescriptor[] props = null;
  52.             try {
  53.                 props = Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors();
  54.             } catch (IntrospectionException e) {
  55.             }
  56.             if (props != null) {
  57.                 for (int i = 0; i < props.length; i++) {
  58.                     try {
  59.                         String name = object2json(props[i].getName());
  60.                         String value = object2json(props[i].getReadMethod().invoke(bean));
  61.                         json.append(name);
  62.                         json.append(":");
  63.                         json.append(value);
  64.                         json.append(",");
  65.                     } catch (Exception e) {
  66.                     }
  67.                 }
  68.                 json.setCharAt(json.length() - 1'}');
  69.             } else {
  70.                 json.append("}");
  71.             }
  72.             return json.toString();
  73.         }
  74.         /**
  75.          * @param list list对象
  76.          * @return String
  77.          */
  78.         public static String list2json(List<?> list) {
  79.             StringBuilder json = new StringBuilder();
  80.             json.append("[");
  81.             if (list != null && list.size() > 0) {
  82.                 for (Object obj : list) {
  83.                     json.append(object2json(obj));
  84.                     json.append(",");
  85.                 }
  86.                 json.setCharAt(json.length() - 1']');
  87.             } else {
  88.                 json.append("]");
  89.             }
  90.             return json.toString();
  91.         }
  92.         /**
  93.          * @param array 对象数组
  94.          * @return String
  95.          */
  96.         public static String array2json(Object[] array) {
  97.             StringBuilder json = new StringBuilder();
  98.             json.append("[");
  99.             if (array != null && array.length > 0) {
  100.                 for (Object obj : array) {
  101.                     json.append(object2json(obj));
  102.                     json.append(",");
  103.                 }
  104.                 json.setCharAt(json.length() - 1']');
  105.             } else {
  106.                 json.append("]");
  107.             }
  108.             return json.toString();
  109.         }
  110.         /**
  111.          * @param map map对象
  112.          * @return String
  113.          */
  114.         public static String map2json(Map<?, ?> map) {
  115.             StringBuilder json = new StringBuilder();
  116.             json.append("{");
  117.             if (map != null && map.size() > 0) {
  118.                 for (Object key : map.keySet()) {
  119.                     json.append(object2json(key));
  120.                     json.append(":");
  121.                     json.append(object2json(map.get(key)));
  122.                     json.append(",");
  123.                 }
  124.                 json.setCharAt(json.length() - 1'}');
  125.             } else {
  126.                 json.append("}");
  127.             }
  128.             return json.toString();
  129.         }
  130.         /**
  131.          * @param set 集合对象
  132.          * @return String
  133.          */
  134.         public static String set2json(Set<?> set) {
  135.             StringBuilder json = new StringBuilder();
  136.             json.append("[");
  137.             if (set != null && set.size() > 0) {
  138.                 for (Object obj : set) {
  139.                     json.append(object2json(obj));
  140.                     json.append(",");
  141.                 }
  142.                 json.setCharAt(json.length() - 1']');
  143.             } else {
  144.                 json.append("]");
  145.             }
  146.             return json.toString();
  147.         }
  148.         /**
  149.          * @param s 参数
  150.          * @return String
  151.          */
  152.         public static String string2json(String s) {
  153.             if (s == null)
  154.                 return "";
  155.             StringBuilder sb = new StringBuilder();
  156.             for (int i = 0; i < s.length(); i++) {
  157.                 char ch = s.charAt(i);
  158.                 switch (ch) {
  159.                 case '"':
  160.                     sb.append("///"");
  161.                     break;
  162.                 case '//':
  163.                     sb.append("");
  164.                     break;
  165.                 case '/b':
  166.                     sb.append("//b");
  167.                     break;
  168.                 case '/f':
  169.                     sb.append("//f");
  170.                     break;
  171.                 case '/n':
  172.                     sb.append("//n");
  173.                     break;
  174.                 case '/r':
  175.                     sb.append("//r");
  176.                     break;
  177.                 case '/t':
  178.                     sb.append("//t");
  179.                     break;
  180.                 case '/':
  181.                     sb.append("///");
  182.                     break;
  183.                 default:
  184.                     if (ch >= '/u0000' && ch <= '/u001F') {
  185.                         String ss = Integer.toHexString(ch);
  186.                         sb.append("//u");
  187.                         for (int k = 0; k < 4 - ss.length(); k++) {
  188.                             sb.append('0');
  189.                         }
  190.                         sb.append(ss.toUpperCase());
  191.                     } else {
  192.                         sb.append(ch);
  193.                     }
  194.                 }
  195.             }
  196.             return sb.toString();
  197.         }
  198.   }

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值