Java 如何在运行时将属性从bean排除到json

Java 如何在运行时将属性从bean排除到json,java,json-lib,Java,Json Lib,我想在运行时使用json库将属性从bean排除到json。 我该怎么做? 我已经尝试过使用jsonconfig的propertyFilter,我不确定它是否在运行时使用。这里有一个基于示例代码的代码片段,可能会有用 PropertyFilter pf = new PropertyFilter(){ public boolean apply( Object source, String name, Object value ) { if( value != null &a

我想在运行时使用json库将属性从bean排除到json。
我该怎么做?

我已经尝试过使用jsonconfig的propertyFilter,我不确定它是否在运行时使用。

这里有一个基于示例代码的代码片段,可能会有用

PropertyFilter pf = new PropertyFilter(){  
   public boolean apply( Object source, String name, Object value ) {  
      if( value != null && Number.class.isAssignableFrom( value.getClass() ) ){  
         return true;  
      }  
      return false;  
   }  
};

PrimitiveBean bean = new PrimitiveBean();  
JsonConfig jsonConfig = new JsonConfig();  
jsonConfig.setJsonPropertyFilter(pf); 
JSONObject json = JSONObject.fromObject( bean, jsonConfig );  
在将bean序列化为JSON对象之前,可以为JSON配置设置不同的函数。。。以防这就是你所说的运行时