日期:2014-05-16  浏览次数:20370 次

java对象转换为JSON字符串
package com.javaeedev.util;

import java.util.Map;
import java.util.Set;

/**
 * Util for converting Java object to JSON string.
 * 
 * @author Xuefeng
 */
public final class JsonUtil {

    /**
     * Convert a Java object to JSON string.
     */
    @SuppressWarnings("unchecked")
    public static String toJson(Object o) {
        if (o==null)
            return "null";
        if (o instanceof String)
            return string2Json((String)o);
        if (o instanceof Boolean)
            return boolean2Json((Boolean)o);
        if (o instanceof Number)
            return number2Json((Number)o);
        if (o instanceof Map)
            return map2Json((Map<String, Object>)o);
        if (o instanceof Object[])
            return array2Json((Object[])o);
        if (o instanceof int[])
            return intArray2Json((int[])o);
        if (o instanceof boolean[])
            return booleanArray2Json((boolean[])o);
        if (o instanceof long[])
            return longArray2Json((long[])o);
        if (o instanceof float[])
            return floatArray2Json((float[])o);
        if (o instanceof double[])
            return doubleArray2Json((double[])o);
        if (o instanceof short[])
            return shortArray2Json((short[])o);
        if (o instanceof byte[])
            return byteArray2Json((byte[])o);
        throw new RuntimeException("Unsupported type: " + o.getClass().getName());
    }

    static String array2Json(Object[] array) {
        if (array.length==0)
            return "[]";
        StringBuilder sb = new StringBuilder(array.length << 4);
        sb.append('[');
        for (Object o : array) {
            sb.append(toJson(o));
            sb.append(',');
        }
        // set last ',' to ']':
        sb.setCharAt(sb.length()-1, ']');
        return sb.toString();
    }

    static String intArray2Json(int[] array) {
        if (array.length==0)
            return "[]";
        StringBuilder sb = new StringBuilder(array.length << 4);
        sb.append('[');
        for (int o : array) {
            sb.append(Integer.toString(o));
            sb.append(',');
        }
        // set last ',' to ']':
        sb.setCharAt(sb.length()-1, ']');
        return sb.toString();
    }

    static String longArray2Json(long[] array) {
        if (array.length==0)
            return "[]";
        StringBuilder sb = new StringBuilder(array.length << 4);
        sb.append('[');
        for (long o : array) {
            sb.append(Long.toString(o));
            sb.append(',');
        }
        // set last ',' to ']':
        sb.setCharAt(sb.length()-1, ']');
        return sb.toString();
    }

    static String booleanArray2Json(boolean[] array) {
        if (array.length==0)
            return "[]";
        StringBuilder sb = new StringBuilder(array.length << 4);
        sb.append('[');
        for (boolean o : array) {
            sb.append(Boolean.toString(o));
            sb.append(',');
        }
        // set last ',' to ']':
        sb.setCharAt(sb.length()-1, ']');
        return sb.toString();
    }

    static String floatArray2Json(float[] array) {
        if (array.length==0)
            return "[]";
        StringBuilder sb = new StringBuilder(array.length << 4);
        sb.append('[');
        for (float o : array) {
            sb.append(Float.toString(o));
            sb.append(',');
        }
        // set last ',' to ']':
        sb.setCharAt(sb.length()-1, ']');
        return sb.toString();
    }

    static String doubleArray2Json(double[] array) {
        if (array.length==0)
            return "[]";
        StringBuilder sb = new StringBuilder(array.length << 4);
        sb.append('[');
        for (double o : array) {
            sb.append(Double.toString(o));
            sb.append(',');
        }
        // set last ',' to ']':
        sb.setCharAt(sb.length()-1, ']');
        return