简体   繁体   English

如何使用Jackson序列化/反序列化ASP.NET JSON日期?

[英]How to serialize/deserialize an ASP.NET JSON date using Jackson?

I'm having some difficulty trying to get Jackson to serialize/deserialize JSON date strings sent from an ASP.NET service. 我在尝试让Jackson序列化/反序列化从ASP.NET服务发送的JSON日期字符串时遇到一些困难。 The string is in the following format: 字符串格式如下:

/Date(1234567890123)/

A simple example of the type of output I get from the server is like the following: 我从服务器获取的输出类型的一个简单示例如下:

{
    "name" : "Bob Marley",
    "birthdate" : "/Date(1234567890123)/"
}

How can I get Jackson to convert the date to a Java date object? 如何获得Jackson将日期转换为Java日期对象? Is there a way to do it without having to write a custom serializer/deserializer? 有没有一种方法可以不必编写自定义的序列化器/反序列化器?

Your best bet is to write a custom deserializer. 最好的选择是编写一个自定义解串器。 Or alternatively, to store the string representation of the date in your bean, but provide an alternative getter that converts the string to a date using a DateFormat instance. 或者,将日期的字符串表示形式存储在bean中,但提供一个替代的getter方法,该方法使用DateFormat实例将字符串转换为日期。 The first option is cleaner and more efficient. 第一种选择是更清洁,更高效。

See question previously asked here on SO . 请参阅先前在此处询问的问题

I think the ASP.NET service is producing a strange value for the date and would change that if I could. 我认为ASP.NET服务为该日期生成了一个奇怪的值,如果可以的话,它将改变它。 If I cannot, then writing a customer deserializer is the way to go. 如果我做不到,那么写一个客户反序列化器就是路要走。

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

相关问题 使用jackson序列化、反序列化 - Serialize, deserialize using jackson 解析ASP.NET MVC使用Java中的Jackson JSON库返回日期 - Parsing ASP.NET MVC returned date using Jackson JSON library in Java 当随机生成属性键时,如何使用 Jackson 从/向 POJO 序列化和反序列化 JSON - How to serialize & deserialize JSON from/to POJO using Jackson, when property key randomly generated 如何使用Jackson将Java Enums序列化和反序列化为JSON对象 - How to serialize and deserialize Java Enums as JSON objects with Jackson 如何使用Jackson序列化/反序列化DefaultMutableTreeNode? - How to serialize/deserialize a DefaultMutableTreeNode with Jackson? 如何使用Jackson反序列化JS日期? - How to deserialize JS date using Jackson? 如何使用Jackson API在序列化和反序列化上使用不同的JSONProperty? - How to use different JSONProperty on Serialize & Deserialize using Jackson API? 如何使用 Jackson 和包装器 object 反序列化/序列化字节数组 - how to deserialize / serialize byte array using Jackson and wrapper object Jackson/Gson 将 JavaFX 属性序列化和反序列化为 json - Jackson/Gson serialize and deserialize JavaFX Properties to json Jackson - 将属性序列化/反序列化为JSON值 - Jackson - serialize/deserialize property as JSON value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM