Elm 如何编写解码器来映射自定义数据类型的值列表?
我正在努力为链接列表编写解码器:Elm 如何编写解码器来映射自定义数据类型的值列表?,elm,Elm,我正在努力为链接列表编写解码器: listOfLinksDecoder : Decoder (List JsonLink) listOfLinksDecoder = Decode.map (List JsonLink) (field "Links" <| Decode.list linkDecoder) 找不到变量列表 请注意,我已经成功地为单个链接编写了解码器: linkDecoder : Decoder JsonLink linkDecoder = D
listOfLinksDecoder : Decoder (List JsonLink)
listOfLinksDecoder =
Decode.map (List JsonLink)
(field "Links" <| Decode.list linkDecoder)
找不到变量列表
请注意,我已经成功地为单个链接编写了解码器:
linkDecoder : Decoder JsonLink
linkDecoder =
Decode.map6 JsonLink
(field "Profile" profileDecoder)
(field "Title" Decode.string)
(field "Url" Decode.string)
(field "ContentType" Decode.string)
(field "Topics" <| Decode.list topicDecoder)
(field "IsFeatured" Decode.bool)
您不需要映射,只需执行以下操作:
listOfLinksDecoder : Decoder (List JsonLink)
listOfLinksDecoder =
field "Links" <| Decode.list linkDecoder
listOfLinksDecoder:Decoder(listJSonLink)
列表链接编码器=
字段“Links”为什么不仅仅是listOfLinksDecoder=Decode.list linkDecoder
?我认为那应该是正确的类型。Decode.map
的第一个参数应该是一个函数,但是List JsonLink
是一个类型。
topicLinks : Id -> Topic -> ContentType -> (Result Http.Error (List JsonLink) -> msg) -> Cmd msg
topicLinks providerId topic contentType msg =
let
url =
baseUrl ++ (getId providerId) ++ "/" ++ "topiclinks"
body =
encodeId providerId |> Http.jsonBody
request =
Http.post url body linksDecoder
in
Http.send msg request
listOfLinksDecoder : Decoder (List JsonLink)
listOfLinksDecoder =
field "Links" <| Decode.list linkDecoder