简体   繁体   English

使用LocalDateTime字段将JSON反序列化为对象

[英]Deserializing JSON to Object with LocalDateTime field

I am giving up. 我放弃了 I have looked through all possible SO pages but I can not get it to work. 我已经浏览了所有可能的SO页面,但是无法正常工作。

I have a class ConfigKeyVal like this: 我有一个这样的ConfigKeyVal类:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class ConfigKeyValue {

    private String name;
    private NssConfigDto value;
}

Where Config class looks like this: Config类如下所示:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Config {

    private String name;
    private String source;
    private String destination;
    private int cycle;
    private LocalDateTime fixedTime;
    private LocalDateTime submitDate;
}

I am trying to deserialize JSON array of ConfigKeyVal (top one) objects directly into the ArrayList of mine. 我正在尝试将ConfigKeyVal (顶级)对象的JSON数组直接反序列化到我的ArrayList中。

public class ConfigKeyValueList extends ArrayList<ConfigKeyValue> {
    public ConfigKeyValueList() {
        super();
    }
}

Like this: 像这样:

final Data values = result.results().get("attributes"); // this is an array of ConfigKeyValue objects
ObjectMapper mapper = new ObjectMapper();
ConfigKeyValueList configKeyValueList = new ConfigKeyValueList();
try {
    configKeyValueList = mapper.readValue(values.asText(), ConfigKeyValueList.class);
} catch (IOException e) {
    e.printStackTrace();
}

I have tried using mapper.registerModule(new JavaTimeModule()); 我已经尝试使用mapper.registerModule(new JavaTimeModule()); but that did not help. 但这没有帮助。 Do I have to write my own deserializer for this or is there a valid tool and I am doing it all wrong? 我是否需要为此编写自己的解串器,或者是否有有效的工具,但我做错了所有事情?

The error I am getting is: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.LocalDateTime: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)) 我收到的错误是: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.LocalDateTime: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?))

I am using those jackson dependencies in my gradle file: 我在gradle文件中使用了那些jackson依赖项:

compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-parameter-names', version: '2.9.6'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jdk8', version: '2.9.6'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.6'

EDIT: This is how JSON looks like 编辑:这就是JSON的样子

[
    {
        "name": "kek1",
        "value": {
            "name": "kek1",
            "source": "source",
            "destination": "dest",
            "cycle": 1,
            "fixedTime": {
                "year": 2017,
                "month": "APRIL",
                "dayOfYear": 95,
                "dayOfWeek": "WEDNESDAY",
                "dayOfMonth": 5,
                "monthValue": 4,
                "hour": 4,
                "minute": 20,
                "second": 0,
                "nano": 0,
                "chronology": {
                    "id": "ISO",
                    "calendarType": "iso8601"
                }
            },
            "submitDate": {
                "year": 2017,
                "month": "APRIL",
                "dayOfYear": 95,
                "dayOfWeek": "WEDNESDAY",
                "dayOfMonth": 5,
                "monthValue": 4,
                "hour": 4,
                "minute": 20,
                "second": 0,
                "nano": 0,
                "chronology": {
                    "id": "ISO",
                    "calendarType": "iso8601"
                }
            }
        }
    },
    {
        "name": "kek2",
        "value": {
            "name": "kek2",
            "source": "source",
            "destination": "dest",
            "cycle": 1,
            "fixedTime": {
                "year": 2017,
                "month": "APRIL",
                "dayOfYear": 93,
                "dayOfWeek": "MONDAY",
                "dayOfMonth": 3,
                "monthValue": 4,
                "hour": 5,
                "minute": 10,
                "second": 0,
                "nano": 0,
                "chronology": {
                    "id": "ISO",
                    "calendarType": "iso8601"
                }
            },
            "submitDate": {
                "year": 2017,
                "month": "APRIL",
                "dayOfYear": 93,
                "dayOfWeek": "MONDAY",
                "dayOfMonth": 3,
                "monthValue": 4,
                "hour": 5,
                "minute": 10,
                "second": 0,
                "nano": 0,
                "chronology": {
                    "id": "ISO",
                    "calendarType": "iso8601"
                }
            }
        }
    }
]

First of all, I don't recommend serializing dates like that. 首先,我不建议像这样序列化日期。 I strongly encourage you to stick to the standards and use ISO 8601 , which is endorsed by the RFC 3339 and by the xkcd 1179 : 我强烈建议您坚持使用标准,并使用RFC 3339xkcd 1179认可的ISO 8601

xkcd 1179: ISO 8601


If you have control over the JSON serialization 如果您可以控制JSON序列化

If you use Spring Data MongoDB , you can use MongoCustomConversions to handle the conversion from Date and LocalDateTime for you: 如果使用Spring Data MongoDB ,则可以使用MongoCustomConversions为您处理从DateLocalDateTime进行的转换:

@Configuration
public class MongoConfiguration {

    @Bean
    public MongoCustomConversions customConversions() {
        List<Converter<?, ?>> converters = new ArrayList<>();
        converters.add(new DateToLocalDateTimeConverter());
        converters.add(new LocalDateTimeToDateConverter());
        return new MongoCustomConversions(converters);
    }

    class DateToLocalDateTimeConverter implements Converter<Date, LocalDateTime> {

        @Override
        public LocalDateTime convert(Date source) {
            return source == null ? null : 
                LocalDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
        }
    }

    class LocalDateTimeToDateConverter implements Converter<LocalDateTime, Date> {

        @Override
        public Date convert(LocalDateTime source) {
            return source == null ? null : Date.from(source.toInstant());
        }
    }
}

Then you can use LocalDateTime your in your beans and let Jackson and the JavaTimeModule handle the serialization/deserialization: 然后,您可以在bean中使用LocalDateTime ,让Jackson和JavaTimeModule处理序列化/反序列化:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

// Serialize
List<ConfigKeyValue> list = null;
String json = mapper.writeValueAsString(list);

// Deserialize
TypeReference<List<ConfigKeyValue>> typeRef = new TypeReference<>() {};
list = mapper.readValue(json, typeRef);

Working with what you have 处理您拥有的东西

If you don't have control over the JSON, then you'll need a custom deserializer . 如果您无法控制JSON,则需要一个自定义解串器 The implementation can be like: 实现可以像这样:

public class CustomLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

    @Override
    public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException {

        JsonNode tree = jp.getCodec().readTree(jp);
        int year = tree.get("year").asInt();
        int month = tree.get("monthValue").asInt();
        int dayOfMonth = tree.get("dayOfMonth").asInt();
        int hour = tree.get("hour").asInt();
        int minute = tree.get("minute").asInt();
        int second = tree.get("second").asInt();
        int nano = tree.get("nano").asInt();

        return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nano);
    }
}

Then annotate your fields to use the deserializer defined above: 然后注释您的字段以使用上面定义的反序列化器:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Config {

    // Other fields

    @JsonDeserialize(using = CustomLocalDateTimeDeserializer.class)
    private LocalDateTime fixedTime;

    @JsonDeserialize(using = CustomLocalDateTimeDeserializer.class)
    private LocalDateTime submitDate;
}

And finally parse your JSON document: 最后解析您的JSON文档:

ObjectMapper mapper = new ObjectMapper();

TypeReference<List<ConfigKeyValue>> typeRef = new TypeReference<>() {};
List<ConfigKeyValue> list = mapper.readValue(json, typeRef);

You have to add the module to your ObjectMapper 您必须将模块添加到您的ObjectMapper

private ObjectMapper getMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.INDENT_OUTPUT,false);
    mapper.setSerializationInclusion(Include.NON_NULL);
    mapper.registerModule(new JavaTimeModule());
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    return mapper;
}

Since the last versions of jackson the module is JavaTimeModule, It used to be JSR310Module 由于杰克逊的最新版本是JavaTimeModule,所以以前是JSR310Module

Edit: It should be used for both serialisation/deserialisation, what you mis is maybe mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); 编辑:应该同时用于序列化/反序列化,您可能会误解的是mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);。

The JSON format should be in ISO: JSON格式应为ISO:

{
    "name": "kek2",
    "value": {
        "name": "kek2",
        "source": "source",
        "destination": "dest",
        "cycle": 1,
        "fixedTime": "2018-07-17T15:10:55"
...

Jackson doesn't have a way to deserialize a LocalDateTime object from any JSON string value. Jackson没有办法从任何JSON字符串值反序列化LocalDateTime对象。

Change LocalDateTime object to "String". 将LocalDateTime对象更改为“ String”。

将 json 反序列化为列表<object><div id="text_translate"><p>我有 json:</p><pre> "taxLevels": [{ "code": "VAT", "percentage": 19.0 } ]</pre><p> 这确实是List&lt;TTaxLevel&gt;</p><p> 我有<strong>Model.class</strong> :</p><pre> public class Model{ private final List&lt;TTaxLevel&gt; taxLevels; }</pre><p> 和<strong>TtaxLevel.class</strong> :</p><pre> @NoArgsConstructor public class TTaxLevel { private String code; private Double percentage; }</pre><p> 但我在这里收到错误:</p><blockquote><p> [Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.util.LinkedHashMap&lt;java.lang.String,java.lang.String&gt; out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.LinkedHashMap&lt;java.lang.String,java.lang.String&gt; out of START_ARRAY token at [Source: (PushbackInputStream); 行:36,列:19](通过参考链:...Model["taxLevels"])]]</p></blockquote><p> 我可以以某种方式强制jackson在这里ArrayList而不是Map吗? 这是一个问题。</p><p> 这是反序列化代码:</p><pre> Model model = new ObjectMapper().readValue(content, Model.class);</pre></div></object> - Deserializing json as List<Object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用GSON将JSON对象反序列化为字符串字段 - Deserializing JSON object to string field with GSON 如何将java对象转换为简单的json字符串而不将LocalDateTime字段转换为扩展的json对象? - How to convert a java object to simple json string without converting the LocalDateTime field to an extended json object? 使用Gson在java中将LocalDateTime序列化和反序列化为RFC 3339格式的JSON - Serializing and deserializing LocalDateTime into JSON in RFC 3339 format in java with Gson 反序列化JSON对象 - Deserializing JSON object 将 json 反序列化为列表<object><div id="text_translate"><p>我有 json:</p><pre> "taxLevels": [{ "code": "VAT", "percentage": 19.0 } ]</pre><p> 这确实是List&lt;TTaxLevel&gt;</p><p> 我有<strong>Model.class</strong> :</p><pre> public class Model{ private final List&lt;TTaxLevel&gt; taxLevels; }</pre><p> 和<strong>TtaxLevel.class</strong> :</p><pre> @NoArgsConstructor public class TTaxLevel { private String code; private Double percentage; }</pre><p> 但我在这里收到错误:</p><blockquote><p> [Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.util.LinkedHashMap&lt;java.lang.String,java.lang.String&gt; out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.LinkedHashMap&lt;java.lang.String,java.lang.String&gt; out of START_ARRAY token at [Source: (PushbackInputStream); 行:36,列:19](通过参考链:...Model["taxLevels"])]]</p></blockquote><p> 我可以以某种方式强制jackson在这里ArrayList而不是Map吗? 这是一个问题。</p><p> 这是反序列化代码:</p><pre> Model model = new ObjectMapper().readValue(content, Model.class);</pre></div></object> - Deserializing json as List<Object> 将 LocalDateTime Object 转换为 LocalDateTime - Converting LocalDateTime Object to LocalDateTime 基于对象类型反序列化JSON - Deserializing JSON based on object type 使用列表属性将JSON反序列化为Object - Deserializing JSON into Object with list attribute 用杰克逊反序列化嵌套的json对象 - Deserializing nested json object with Jackson 将多态 Json object 反序列化为 POJO - Deserializing a polymorphic Json object to a POJO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM