使用JSON.toJSONString(str,SerializerFeature... serial);
SerializerFeature
QuoteFieldNames———-输出key时是否使用双引号,默认为true
WriteMapNullValue——–是否输出值为null的字段,默认为false
WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
WriteNullListAsEmpty—–List字段如果为null,输出为[],51);">WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,255);">
WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; public class JavaToJson { public static void main(String[] args) { Class<?> c; Object o; try { c = Class.forName("com.free.User"); o = c.newInstance(); String json = JSON.toJSONString(o,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty); System.out.println(json); } catch (Exception e) { } } }