1.实体上json
@JsonInclude(Include.NON_NULL)app
//将该标记放在属性上,若是该属性为NULL则不参与序列化
//若是放在类上边,那对这个类的所有属性起做用
//Include.Include.ALWAYS 默认
//Include.NON_DEFAULT 属性为默认值不序列化
//Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
//Include.NON_NULL 属性为NULL 不序列化spa
2.代码上
ObjectMapper mapper = new ObjectMapper();对象
mapper.setSerializationInclusion(Include.NON_NULL); input
//经过该方法对mapper对象进行设置,全部序列化的对象都将按改规则进行系列化
//Include.Include.ALWAYS 默认
//Include.NON_DEFAULT 属性为默认值不序列化
//Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
//Include.NON_NULL 属性为NULL 不序列化it
User user = new User(1,"",null);
String outJson = mapper.writeValueAsString(user);
System.out.println(outJson);io
3.SerializationFeature.INDENT_OUTPUT:是否缩放排列输出,默认false,有些场合为了便于排版阅读则须要对输出作缩放排列coding
4. 文件转Json List示例List
private static List<JSONObject> parseFromJson(String jsonFileName) { File f = new File("D:/rule/" + jsonFileName); InputStream inputStream = null; try { inputStream = new FileInputStream(f); JacksonCustomObjectMapper jacksonCustomObjectMapper = new JacksonCustomObjectMapper(); List<JSONObject> jsonList = jacksonCustomObjectMapper.readValue(inputStream, new TypeReference<List<JSONObject>>() { }); return jsonList; } catch (UnsupportedEncodingException e) { return null; } catch (Exception e) { return null; } }