【问题标题】:Debugging a JSON decoder调试 JSON 解码器
【发布时间】:2021-02-11 13:12:09
【问题描述】:

我正在尝试测试解码器 - 但我只能取回默认值。为文字墙道歉,但是当我尝试较小的示例时,它们总是有效,所以我猜这里某处存在愚蠢的错误。

我一直在试图弄清楚为什么这在很长一段时间内都行不通,但没有运气。 JSON 似乎是有效的(我尝试在 JS 和在线验证器中解析它)。

我尝试了不同的 JSON 解码方法,但还是没有成功。

非常感谢任何帮助。如果应该在问题中添加任何其他内容,请告诉我(我是 elm 新手,如果你不知道的话)。

我正在尝试解码如下所示的 JSON:

                        {
                            "fade": 1,
                            "colour": "Campbells Red",
                            "stock": 1,
                            "site": "",
                            "url": "",
                            "plastic":"DX",
                            "name":"aviar",
                            "seenAt":1612884837886,
                            "weight":175,
                            "compositeIdentifier":"aviar||Innova||DX||Campbells Red||175",
                            "manufacturer":"Innova",
                            "expiresAt":1615476837886,
                            "glide":3,
                            "turn":0,
                            "speed":2,
                            "price":8.99
}

我的类型是这样的:

type alias DiscSighting =
    { fade: Int
    , colour: String
    , stock: Int
    , site: String
    , url: String
    , plastic: String
    , name: String
    , weight: Int
    , compositeIdentifier: String
    , manufacturer: String
    , glide: Int
    , turn: Int
    , speed: Int
    , price: Float
    }

我的解码器看起来像这样:

discDecoder: Decoder DiscSighting
discDecoder =
    succeed DiscSighting
        |> andMap (field "fade" (int) |> (withDefault) -1)
        |> andMap (field "colour" (string) |> (withDefault) "")
        |> andMap (field "stock" (int) |> (withDefault) -1)
        |> andMap (field "site" (string) |> (withDefault) "")
        |> andMap (field "url" (string) |> (withDefault) "")
        |> andMap (field "plastic" (string) |> (withDefault) "")
        |> andMap (field "name" (string) |> (withDefault) "")
        |> andMap (field "weight" (int) |> (withDefault) -1)
        |> andMap (field "compositeIdentifier" (string) |> (withDefault) "")
        |> andMap (field "manufacturer" (string) |> (withDefault) "")
        |> andMap (field "glide" (int) |> (withDefault) -1)
        |> andMap (field "turn" (int) |> (withDefault) -1)
        |> andMap (field "speed" (int) |> (withDefault) -1)
        |> andMap (field "price" (float) |> (withDefault) -1)


我得到的错误是由于测试失败(它返回结果的错误部分,因此测试失败):

Err(失败“期望一个带有名为price的字段的对象 )

【问题讨论】:

  • 您对andMapwithDefault 的定义是什么?你在用Json.Decode.Extra吗?尝试删除withDefaults 之一,看看它给出了什么错误。
  • @absynce 他们来自 Json.Decode.Extra,对不起,我应该说。当我删除 withDefault 时,我得到一个错误,而不是 Ok 响应。下面的答案似乎能够解码对象,但是当我使用相同的代码时,它无法像我拥有的​​那样解码 JSON 字符串
  • 您能否将错误响应的详细信息添加到您的问题中?
  • 我现在已经加了(不过估计用处不大)
  • @absynce 原来我使用了错误的变量...不敢相信

标签: elm


【解决方案1】:

discDecoder 中,我不确定andMapwithDefault 的定义是什么,但是NoRedInk/elm-json-decode-pipeline 包中的optional 函数可以代替andMapwithDefault 工作:

discDecoder : Decoder DiscSighting
discDecoder =
    succeed DiscSighting
        |> optional "fade" int -1
        |> optional "colour" string ""
        |> optional "stock" int -1
        |> optional "site" string ""
        |> optional "url" string ""
        |> optional "plastic" string ""
        |> optional "name" string ""
        |> optional "weight" int -1
        |> optional "compositeIdentifier" string ""
        |> optional "manufacturer" string ""
        |> optional "glide" int -1
        |> optional "turn" int -1
        |> optional "speed" int -1
        |> optional "price" float -1

完整的工作示例:https://ellie-app.com/ckYm8HhMJfKa1

【讨论】:

  • 感谢您的回答 - 我在我的代码上试过这个,虽然我可以看到它可以将 data 解码为一个对象,但我对它应该如何用于解码一个对象感到困惑json字符串。你有什么建议吗?
  • 到目前为止,我可能犯了最愚蠢的错误,并且一直将其指向错误的变量...感谢您为我指明了正确的方向,并向我展示了我真正应该做的事情...只要我使用正确的输入!
  • @OliverRadini 很高兴你能成功!
猜你喜欢
  • 2023-03-24
  • 2012-05-04
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多