https://meilishiyan-song.taobao.com/

Object转换为json格式

  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() ));
     }

 

posted @ 2016-11-11 13:10  望梦圆  阅读(996)  评论(0编辑  收藏  举报
https://meilishiyan-song.taobao.com/