【发布时间】:2015-11-20 07:55:41
【问题描述】:
努力寻找正确的方法来完成我的解码器。我从表单的数据开始
[{_id:'interests', [{obj1}, {obj1}]}
,{_id:'countries', [{obj2}, {...}]}
,{_id:'sections', [{obj3}, {...}]}]
我想去Decoder Summary哪里
type alias Summary =
{ sections : List Section
, interests : List Interest
, countries : List Country
}
到目前为止,我能得到的最好的结果就是这样的输出:
[ Interests (List Interest), Countries (List Country), Sections (List Section)]
但这仍然需要一些相当脆弱的模式匹配(依赖于数组的一致顺序因此对于 0.16 非常有问题)。为此我使用
summaryItemDecoder : String -> Decoder SummaryInfo
summaryItemDecoder item =
let dec =
case item of
"sections" -> Json.map Sections sectionsDecoder
"interests" -> Json.map Interests interestsDecoder
"countries" -> Json.map Countries countriesDecoder
in ("data" := dec)
listSummaryDecoder : Decoder (List SummaryInfo)
listSummaryDecoder =
("_id" := string) `Json.andThen` summaryItemDecoder
|> list
完整代码here。感谢一些最后的提示
【问题讨论】:
标签: elm