在ELM 0.19解码中超过8个地图

Ste*_*n B 5 json elm

我有标志解码器默认值.

flagsDecoder : Decode.Decoder Params
flagsDecoder =
    Decode.map8 Params
        (Decode.field "field1" (Decode.string) |> (Decode.withDefault) "1")
        (Decode.field "field2" (Decode.string)   |> (Decode.withDefault) "2")
        (Decode.field "field3" (Decode.string)   |> (Decode.withDefault) "3")
        (Decode.field "field4" (Decode.string) |> (Decode.withDefault) "4)
        (Decode.field "field5" (Decode.string)  |> (Decode.withDefault) "5")
        (Decode.field "field6" (Decode.int) |> (Decode.withDefault) 6)
        (Decode.field "field7" (Decode.string)    |> (Decode.withDefault) "7")
        (Decode.field "field8" (Decode.string)   |> (Decode.withDefault) "8")
Run Code Online (Sandbox Code Playgroud)

我该如何添加更多字段?我有一个包含10个字段的JSON对象.

gle*_*nsl 13

我会建议使用andMap来自elm-community/json-extra:

flagsDecoder : Decode.Decoder Params
flagsDecoder =
    Decode.succeed Params
        |> Decode.andMap (Decode.field "field1" (Decode.string) |> (Decode.withDefault) "1")
        |> Decode.andMap (Decode.field "field2" (Decode.string)   |> (Decode.withDefault) "2")
        |> Decode.andMap (Decode.field "field3" (Decode.string)   |> (Decode.withDefault) "3")
        |> Decode.andMap (Decode.field "field4" (Decode.string) |> (Decode.withDefault) "4")
        |> Decode.andMap (Decode.field "field5" (Decode.string)  |> (Decode.withDefault) "5")
        |> Decode.andMap (Decode.field "field6" (Decode.int) |> (Decode.withDefault) "6")
        |> Decode.andMap (Decode.field "field7" (Decode.string)    |> (Decode.withDefault) "7")
        |> Decode.andMap (Decode.field "field8" (Decode.string)   |> (Decode.withDefault) "8")
Run Code Online (Sandbox Code Playgroud)

但也有其他选项,如elm-json-decode-pipeline一个新的实验API.andMap是非常简单和直接的,mapN如果需要的话,可以与之一起使用并切换到.