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

  • 相关阅读:
    2. Django每日一码 之as_view() 源码
    gdb
    Mex文件在VS2010中调试方法
    intel ipp6.0安装过程
    C++开源库大全
    Win7下搭建Go语言开发环境
    for_each使用方法详解
    使用VS2012编译和使用C++ STL(STLport)
    django css
    google mock C++单元测试框架
  • 原文地址:https://www.cnblogs.com/songyunxinQQ529616136/p/6053876.html
Copyright © 2011-2022 走看看