首页 > 技术文章 > 将java类转成json对象

zhizhu1120418975 2020-06-11 10:46 原文

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

推荐阅读