我已经阅读了很多关于JSON解析到Java对象的帖子,并且在我引入LocalDateTime之前,我的解析工作正常.我曾尝试使用Java 8解析器,JSR310模块并构建自定义 - 下面描述每个解决方案的障碍.任何帮助,将不胜感激!
这是我的JSON字符串,由杰克逊从另一个POJO创建:
{"validEscortsWTheirSpecReqs":"MAYBE", "modifiedDateTimeNeedToBeThere": {"dayOfMonth":6,"dayOfWeek":"MONDAY","month":"FEBRUARY","year":2017, "hour":10,"minute":1,"second":24,"nano":0,"dayOfYear":37,"monthValue":2, "chronology":{"id":"ISO","calendarType":"iso8601"} } }
产生
com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected VALUE_STRING: Expected array or string. at ... at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270) at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:1376) at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:118) at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:39) ...
来自:
ComputeArrive响应= mapper.readValue(responseString,ComputeArrive.class);
响应具有以下字段:
记录器记录器//不应该由JSON映射,没有getters/setters静态最终长serialVersionUID = 1L; //"String validEscortsWTheirSpecReqs ="YES"//想要映射的LocalDateTime modifiedDateTimeNeedToBeThere //想要映射
我现在可能已经注册了太多的模块,添加它们以试图让它解析:
mapper.findAndRegisterModules(); //mapper .registerModule(new ParameterNamesModule()); mapper.registerModule(new Jdk8Module()); mapper.registerModule(new JavaTimeModule()); mapper.registerModule(new JSR310Module());
所以我试图将Spring Boot依赖项添加到我的项目中,并为JSR310编写一个自定义程序(鉴于上述模块,这似乎是不必要的):
@Configuration public class JacksonConfig { @Bean @Primary public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); return objectMapper; } }
我的应用程序有很多依赖项,所以我不想让它成为它的"父",并添加了以下依赖项.但是,找不到"Jackson2ObjectMapperBuilder"类,所以上面没有编译这些依赖项:
org.springframework spring-core 4.3.5.RELEASE org.springframework spring-web 4.3.5.RELEASE org.springframework spring-beans 4.3.5.RELEASE org.springframework spring-context 4.3.5.RELEASE org.springframework spring-expression 4.3.5.RELEASE org.springframework spring-aop 4.3.5.RELEASE aopalliance aopalliance 1.0
我没有找到很多关于"Jackson2ObjectMapperBuilderCustomizer"的文档,它可能是"Jackson2ObjectMapperBuilder"的替代品 - 这可能就是问题所在.请注意,上面的类没有被我实例化,因为我假设Spring实例化它.
任何关于如何解析我的回答的想法都将非常感激!