gpt4 book ai didi

json - 如何解码 Elm 中的静态值

转载 作者:行者123 更新时间:2023-12-01 23:21:59 24 4
gpt4 key购买 nike

使用 Json.decode 是否可以解码静态值,比如 Json 不包含值,但我希望本地类型别名具有更多具有默认值的数据。我怎样才能做到这一点?谢谢!

最佳答案

有两种方法可以做到这一点,

使用Decode.succeeded

import Json.Decode as Decode

-- snip

Decode.map2 Item
(Decode.field "title" Decode.string)
(Decode.succeed 0)

(其中 Item 是任意的类型别名)


或者使用闭包

Decode.map (\name -> {name = name, age = 42} ) (Decode.field "name" Decode.string)

关于json - 如何解码 Elm 中的静态值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67975931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com