1
我想在運行時使用json-lib將屬性從bean中排除到json上。
我該怎麼做?
我已經嘗試使用jsonconfig的propertyFilter,我不知道它是否在運行時。如何在運行時將屬性從bean排除到json上
我想在運行時使用json-lib將屬性從bean中排除到json上。
我該怎麼做?
我已經嘗試使用jsonconfig的propertyFilter,我不知道它是否在運行時。如何在運行時將屬性從bean排除到json上
這是一個基於Filtering Properties in JSON Advanced Features示例代碼的代碼片段,可能有用。
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配置。