本文介绍了用aeson解析utctime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过eson解析UTCTime值.我试图对其中一个进行编码并将其反馈,但是那没有用:

I can't get aeson to parse an UTCTime value. I tried to encode one and feed it back, but that didn't work:

Prelude Data.Aeson Data.Time.Clock> getCurrentTime >>= (print . encode)
"\"2013-10-17T09:42:49.007Z\""
Prelude Data.Aeson Data.Time.Clock> decode "2013-10-17T09:42:49.007Z" :: Maybe UTCTime
Nothing
Prelude Data.Aeson Data.Time.Clock> decode "\"2013-10-17T09:42:49.007Z\"" :: Maybe UTCTime
Nothing

UTCTime类型的FromJSON实例如下():

The FromJSON instance of the UTCTime type is the following (ref):

instance FromJSON UTCTime where
    parseJSON = withText "UTCTime" $ \t ->
        case parseTime defaultTimeLocale "%FT%T%QZ" (unpack t) of
          Just d -> pure d
          _      -> fail "could not parse ISO-8601 date"

此处找到格式说明之后,没事.我想念什么?

following the format description found here, everything should be ok. What am I missing?

推荐答案

Prelude Data.Aeson Data.Time> decode (encode [x]) :: Maybe [UTCTime]
Just [2013-10-17 10:06:59.542 UTC]

请注意黑线码头中的陷阱"部分:

Note the "pitfalls" section in the haddocks:

...

这篇关于用aeson解析utctime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 22:26