ObjectMapper&Splitter Myth丶恋晨 2022-12-20 03:30 114阅读 0赞 public class Test { public static void main(String[] args) throws IOException { testJson2Time(); // testSplitter(); } /** * Splitter */ public static void testSplitter() { Splitter splitter = Splitter.on(",").trimResults().omitEmptyStrings(); String source = "aa, bb , cc CC, dd"; List<String> splitList = Lists.newArrayList(splitter.split(source)); System.out.println(Joiner.on("-").join(splitList)); } /** * ObjectMapper * * @throws IOException */ public static void testJson2Time() throws IOException { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setSerializationInclusion(Include.NON_NULL); objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer( DateTimeFormatter.ofPattern("HH:mm:ss"))); objectMapper.registerModule(javaTimeModule); String jsonStr = "[{\"start\":\"10:00\",\"end\":\"11:00\",\"bb\":\"bb\"},{\"start\":\"12:00\",\"end\":\"13:00\"}]"; List<TimeData> timeDataList = objectMapper .readValue(jsonStr, new TypeReference<ArrayList<TimeData>>() { } ); System.out.println(timeDataList); String str = objectMapper.writeValueAsString(timeDataList); System.out.println(str); } @Data private static class TimeData { @JsonFormat(pattern = "HH:mm") private LocalTime start; @JsonFormat(pattern = "HH:mm") private LocalTime end; private String a; } }
还没有评论,来说两句吧...