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

解析json和封装json
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

public final class JsonUtil
{

        /**
         * 把对象封装为JSON格式
         *
         * @param o
         *            对象
         * @return JSON格式
         */
        @SuppressWarnings("unchecked")
        public static String toJson(final Object o)
        {
                if (o == null)
                {
                        return "null";
                }
                if (o instanceof String) //String
                {
                        return string2Json((String) o);
                }
                if (o instanceof Boolean) //Boolean
                {
                        return boolean2Json((Boolean) o);
                }
                if (o instanceof Number) //Number
                {
                        return number2Json((Number) o);
                }
                if (o instanceof Map) //Map
                {
                        return map2Json((Map<String, Object>) o);
                }
                if (o instanceof Collection) //List  Set
                {
                        return collection2Json((Collection) o);
                }