【发布时间】:2018-07-22 05:20:12
【问题描述】:
我刚开始学习 Elm,但遇到了障碍。向这个很棒的社区寻求帮助。
我正在寻找对嵌套 json 进行解码并将特定嵌套值拉入 elm 记录的方法。
json 源码如下所示:
{
"id": 672761,
"modified": "2018-02-12T00:53:04",
"slug": "Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.",
"type": "post",
"link": "https://awesomelinkhere.com",
"title": {
"rendered": "Sed posuere consectetur est at lobortis."
},
"content": {
"rendered": "Nulla vitae elit libero, a pharetra augue.",
},
"excerpt": {
"rendered": "Donec sed odio dui.",
}
}
我想将title.rendered 和content.rendered 拆分为模型中的一个字段,模型如下所示:
type alias Post =
{ id : Int
, published : String
, title : String
, link : String
, slug : String
, excerpt : String
}
我的幼稚解码器看起来像这样
postDecoder : Decoder Post
postDecoder =
Decode.map6 Post
(field "id" Decode.int)
(field "modified" Decode.string)
(field "title" Decode.string)
(field "link" Decode.string)
(field "slug" Decode.string)
(field "excerpt" Decode.string)
【问题讨论】:
-
将您的更新添加为答案并将其标记为已解决。它使其他人以后更容易找到这些信息。
-
更新了更新!
标签: elm