将java类转成json对象

public static  <T, t> String classToJson(Class clazz) throws IllegalAccessException, InstantiationException {
        SerializerFeature[] serializer = {SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullBooleanAsFalse};

        Class<t> c= clazz;
        T t= (T) c.newInstance();
        String s = JSONObject.toJSONString(t, serializer);
        return s;
    }

借助fastjson

posted @ 2020-06-11 10:46  《》  阅读(1698)  评论(0编辑  收藏  举报