fastjson中有個注解@JSONField(serialize = false)可以在使用fastjson進(jìn)行序列化時來過濾屬性字段,具體用法是直接在實(shí)體類需要過濾的屬性字段上方添加該注解即可。
方法一、fastjson的注解
fastjson中有個注解@JSONField(serialize = false)可以在使用fastjson進(jìn)行序列化時來過濾屬性字段,具體用法是直接在實(shí)體類需要過濾的屬性字段上方添加該注解即可,如下:
@JSONField(serialize = false)
private String ccc;
方法二、Java關(guān)鍵字
其實(shí)Java自帶的有一個屬性關(guān)鍵字transient是忽略序列化的,如下:
private transient String password;
使用該關(guān)鍵字進(jìn)行修飾的字段是不參與序列化的,所以在使用fastjson轉(zhuǎn)JSON字符串時就可以忽略掉該字段。
但是因?yàn)槭荍ava關(guān)鍵字是忽略所有的序列化的,所以也會影響該字段其他的序列化操作,如果會用到其他的序列化操作并且不需要忽略該字段的話,建議使用方法一。
方法三、最靈活 fastjson的屬性名過濾器
com.alibaba.fastjson.JSONObject
private static SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
private static SerializeFilter[] filters = null;
static {
filter.getExcludes().add("enumValue");
filter.getExcludes().add("defaultValue");
filter.getExcludes().add("minValue");
filter.getExcludes().add("maxValue");
filter.getExcludes().add("decimalLength");
filters = new SerializeFilter[]{filter};
}
StringBuilder jsonBuilder = new StringBuilder();
for(CustomizeStrategyFieldVO vo:voList){
jsonBuilder.append(
JSONObject.toJSONString(vo, filters, SerializerFeature.WriteMapNullValue)
);
}