欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-12 18:18:08
如下所示: /* * created on nov 4, 2016 * * todo to change the template for this...

如下所示:

/*
 * 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 自定义使用函数的简单实例全部内容了,希望大家多多支持~