public String toJSONString(Object obj)
{
SerializeWriter serializewriter = new SerializeWriter();
JSONSerializer jsonserializer = new JSONSerializer(serializewriter);
jsonserializer.config(SerializerFeature.WriteNullStringAsEmpty, true);
jsonserializer.config(SerializerFeature.WriteMapNullValue, true);
jsonserializer.config(SerializerFeature.DisableCircularReferenceDetect, true);
jsonserializer.config(SerializerFeature.WriteNullListAsEmpty, true);
jsonserializer.write(obj);
return serializewriter.toString().replaceAll("":null", "":""");
}
测试:
public static void main(String [] args){
JsonNull2SpaceSerializationImpl json = new JsonNull2SpaceSerializationImpl();
Map result = new HashMap();
Map tmp = null;
List list = null;
result.put("nullString",null);
result.put("String","nullsd");
result.put("nullMap",tmp);
result.put("nullList",list);
System.out.println(1+json.toJSONString(result));
System.out.println(json.toJSONString(new JsonNull2SpaceSerializationImpl().new TestDTO() ));
}