jackson 转json. 过滤null值
@Test
public void tttttt() throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
// 过滤对象的null属性.
mapper.setSerializationInclusion(Inclusion.NON_NULL);
// 过滤map中的null值
mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);
T1 t1 = new T1();
t1.setT(null);
t1.setT1("asdfa");
Map<String, Object> map = new HashMap<String, Object>();
map.put("asdf", t1);
map.put("fff", null);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
list.add(null);
list.add(map);
String ret_val = mapper.writeValueAsString(list);
System.out.println(ret_val);
}
输出结果:
[null,{"asdf":{"t1":"asdfa"}}]
---
注: list好像没有可以配置除null.
---
list中的null在 org.codehaus.jackson.map.SerializationConfig.Feature 中没有发现list的设置有null的开关.
但有一个是否输出空List的属性.(就是list有,但里面没有内容)的WRITE_EMPTY_JSON_ARRAYS
--
设置jackson的很多属性可以在 org.codehaus.jackson.map.SerializationConfig.Feature和org.codehaus.jackson.map.annotate.JsonSerialize$Inclusion
里找.