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; } }