无法使用JSON解析MySql TImestamp

时间:2013-03-20 00:29:33

标签: mysql json parsing serialization timestamp

尝试解析以下JSON字符串时:

{
    marketplaceId:"MKPL",
    asin:"ASIN1",
    sourceTimestamp:2013-03-19T23:38:24.054Z,
    orderId:"ORD1",
    vendorId:"SUPR1",
    warehouseId:"SEA8",
    inventoryOwnerGroup:376,
    lastUpdatedAt:2013-03-19T23:38:23.919Z,
    isHighConfidence:true,
    quantityArriving:2,
    expectedDeliveryDate:2013-03-19T23:38:23.919Z
}

我得到以下异常:

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.sql.Timestamp out of VALUE_EMBEDDED_OBJECT token
 at [Source: N/A; line: -1, column: -1] (through reference chain: com.amazon.freshwombat.po.PurchaseRecord["lastUpdatedAt"])
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
    at org.codehaus.jackson.map.deser.std.StdDeserializer._parseDate(StdDeserializer.java:580)
    at org.codehaus.jackson.map.deser.std.TimestampDeserializer.deserialize(TimestampDeserializer.java:28)
    at org.codehaus.jackson.map.deser.std.TimestampDeserializer.deserialize(TimestampDeserializer.java:19)

我错过了什么吗?谢谢!

2 个答案:

答案 0 :(得分:3)

JSON不支持“日期”的概念。它只支持simple data types类似字符串,数字,数组,布尔值等。因此,将日期表示为字符串。 E.g:

lastUpdatedAt: "2013-03-19T23:38:23.919Z",

您必须使用其他JavaScript工具/第三方库进行实际日期解析。

答案 1 :(得分:1)

如果您使用 Jackson 进行JSon to Object转换,则必须指定DateFormat一个mapper实例应使用以便从表示日期的String值生成Date或timestamp

如果是(mysqltimestamp

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(format)
Entity entity = mapper.readValue("{ }", Entity.class);

如此密切关注@SimpleCoder的建议,我想你不应该有这个问题。