Json 自定义使用函数的简单实例

如下所示:

/*
 * Created on Nov 4, 2016
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.suning.commerce.util;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

/**
 * @author nicholas tse
 * 
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
 */
public class JsonUtils {
  /**
   * 
   * @param array
   * @return
   */
  private static String array2Json(Object[] array) {
    if (array.length == 0)
      return "[]";
    int i = array.length;
    StringBuffer sb = new StringBuffer(i << 4);
    sb.append('[');
    for (int j = 0; j < i; j++) {
      Object o = array[j];
      sb.append(toJson(o));
      sb.append(',');
    }
    // 将最后添加的 ',' 变为 ']':
    sb.setCharAt(sb.length() - 1, ']');
    return sb.toString();
  }

  private static String string2Json(String s) {
    StringBuffer sb = new StringBuffer(s.length() + 20);
    sb.append('\"');
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      switch (c) {
      case '\"':
        sb.append("\\\"");
        break;
      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;
      default:
        sb.append(c);
      }
    }
    sb.append('\"');
    return sb.toString();
  }

  public static String toJson(Object o) {
    if (o == null) {
      return "null";
    } else if (o instanceof String) {
      return string2Json((String) o);
    } else if ((o instanceof Boolean) || (o instanceof Number)) {
      return o.toString();
    } else if ((o instanceof Date) || (o instanceof Time)||o instanceof Timestamp) {
      return o.toString();
    } else if (o instanceof java.util.Date) {
      return DateUtil.formatDate((java.util.Date)o,"yyyy-MM-dd HH:mm:ss");
    } else if (o instanceof Map) {
      return map2Json((Map) o);
    } else if (o instanceof Object[]) {
      return array2Json((Object[]) o);
    } else if (o instanceof Collection) {
      return array2Json(((Collection) o).toArray());
    } else {
      try {
        Map describe = BeanUtils.describe(o);
        return map2Json(describe);
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    throw new RuntimeException("Unsupported type: " + o.getClass().getName());
  }

  /**
   * 
   * @param map
   * @return
   */
  private static String map2Json(Map map) {
    if (map.isEmpty())
      return "{}";
    StringBuffer sb = new StringBuffer(map.size() << 4);
    sb.append('{');
    Iterator iterator = map.keySet().iterator();
    while (iterator.hasNext()) {
      Object key = iterator.next();
      Object value = map.get(key);
      sb.append('\"');
      sb.append(key);
      sb.append('\"');
      sb.append(':');
      sb.append(toJson(value));
      sb.append(',');
    }
    // 将最后的 ',' 变为 '}':
    sb.setCharAt(sb.length() - 1, '}');
    return sb.toString();
  }
}

以上就是小编为大家带来的Json 自定义使用函数的简单实例全部内容了,希望大家多多支持菜鸟教程(cainiaojc.com)~