我有一条从JSON解析的记录:
import Json.Decode exposing (..)
import Json.Decode.Pipeline exposing (..)
type alias Article = {
pubDate: String
}
articleDecoder : Decoder Article
articleDecoder =
decode Article
|> required "pubDate" string
现在,如果我要使用Date
forpubDate
而不是原始字符串,如何更改代码以“填充” JSON值Date.fromString
?
Elm词汇表中没有膨胀这样的术语。
您解码JSON字符串或JavaScritpt对象。
榆木中没有物体。
因此,您想将具有格式化日期的字符串解码为Date类型的数据结构。
截止到Today(0.18.0
),事实证明,来自core的Date.fromString是不可靠的。
您应该使用Date.Extra.fromIsoString从justinmimbs / ELM-日期额外的模块实现更可靠的日期解析从ISO 8601。
为了清楚起见,我保留名称空间。
dateDecoder : Decoder Date
dateDecoder =
Json.Decode.string
|> Json.Decode.andThen
(\s ->
case Date.Extra.fromIsoString s of
Err e ->
Json.Decode.fail e
Ok d ->
Json.Decode.succeed d
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句