当前位置: 移动技术网 > IT编程>开发语言>Java > Json 自定义使用函数的简单实例

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

如下所示:

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网