我正在努力为链接列表编写解码器:
listOfLinksDecoder : Decoder (List JsonLink)
listOfLinksDecoder =
Decode.map (List JsonLink)
(field "Links" <| Decode.list linkDecoder)
Error:
Decode.map (List JsonLink)
Cannot find variable
List
请注意,我已经成功地为单个链接编写了解码器:
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)
请注意,我尝试搜索 this documentation 。但是,我仍然无法找到适合我的案例的示例。
附录:
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
另请注意,无法找到变量列表
错误是因为List
是一种类型,而不是构造函数/函数。
关于elm - 如何编写解码器来映射自定义数据类型的值列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46515227/