1

我想在运行时使用 json-lib 将属性从 bean 排除到 json 中。
我该怎么做?
我试过使用 jsonconfig 的 propertyFilter,我不确定它是否在运行时。

4

1 回答 1

0

这是基于JSON 高级功能中的过滤属性中的示例代码的代码片段,可能有用。

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 Config 设置不同的函数......以防这就是运行时的意思。

于 2011-11-18T13:12:45.340 回答