2013-03-20 61 views
2

解析MySQL的时间戳虽然试图解析以下JSON字符串:无法与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

JSON不支持“日期”的概念。它仅支持simple data types,如字符串,数字,数组,布尔值等。因此,将您的日期表示为字符串。例如:

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

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

1

如果您正在使用杰克逊的JSON转换成对象必须指定一个DateFormat例如mapper应使用以便生成表示日期或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,并在这个片段中我想你不应该有问题的建议。