如何使用jakson将localDate类型对象转换为像gson这样的jsonObject

时间:2018-06-08 08:29:34

标签: java json gson localdate

当我使用Jakson库将对象转换为json对象时,结果json输出如下。

"runDate":{"date":{"hour":11,"minute":5,"second":27,"nano":500000000,"monthValue":6,"year":2018,"dayOfMonth":8,"dayOfWeek":"FRIDAY","dayOfYear":159,"month":"JUNE","chronology":{"id":"ISO","calendarType":"iso8601"}}

但是当我使用库gson时:

"runDate":{"date":{"month":6,"year":2018,"day":8},"time":{"hour":11,"nano":518000000,"minute":9,"second":21}}

问题:如何使用jakson将对象转换为像gson这样的jsonObject

java set LocalDate,
log.setRunDate(LocalDateTime.now(z));

我的pom jakson依赖版:

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 -->
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>2.9.5</version>
    </dependency>

你能帮我看看如何使用jakson

将对象转换为像gson这样的jsonObject

1 个答案:

答案 0 :(得分:0)

如果你有一个与你的json数据匹配的类,你可以这样做,类似于Gson。

// json Object Mapper class
ObjectMapper objectMapper = new ObjectMapper();

//convert json string to object
YourClass emp = objectMapper.readValue(jsonData, YourClass.class);