zoukankan      html  css  js  c++  java
  • 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() ));
         }

  • 相关阅读:
    优化linux,安装node环境
    腾讯云主机的公网无法访问,putty和FileZilla连接不上
    angularjs1.x的directive中的link参数element见解
    springboot自动装配原理
    redis哨兵部署
    HashMap笔记
    多线程与高并发
    java面试必会知识点
    maven仓库
    java任务调度简单示例
  • 原文地址:https://www.cnblogs.com/songyunxinQQ529616136/p/6053876.html
Copyright © 2011-2022 走看看