Elm使用数组解码json对象
我在榆树是全新的。我正在尝试从get response获取数据 现在我正试着为它制作解码器。我有这样的json:Elm使用数组解码json对象,json,elm,Json,Elm,我在榆树是全新的。我正在尝试从get response获取数据 现在我正试着为它制作解码器。我有这样的json: { data: [ { "price" = 300.5 }, { "price" = 1005 } ] } 我所需要的是得到最低的价格,并返回它。(至少返回每个价格) 现在我在Decode.index上叠加 priceDecoder : Decode.Deco
{
data: [
{
"price" = 300.5
},
{
"price" = 1005
}
]
}
我所需要的是得到最低的价格,并返回它。(至少返回每个价格)
现在我在Decode.index上叠加
priceDecoder : Decode.Decoder String
priceDecoder =
Decode.field "data" Decode.list
接下来我该怎么办?这个问题本身有几个问题。首先,您没有发布您收到的错误。我得到的错误是:
The 2nd argument to `field` is not what I expect:
25| Decode.field "data" Decode.list
^^^^^^^^^^^
This `list` value is a:
Decode.Decoder a -> Decode.Decoder (List a)
But `field` needs the 2nd argument to be:
Decode.Decoder a
其次,您发布的JSON无效,数据应包含在引号中,属性和值应以:
分隔,而不是=
分隔
第三,priceDecoder
的类型似乎是错误的,因为JSON不包含String
数据,或者您没有指定还希望将Float
转换为String
。我会假设类型是错误的
因此,您(或至少我)得到的错误表示,list
是一个函数Decoder a->Decoder(list a)
,而它只需要一个Decoder a
值。这是因为list
需要向其传递解码器a
,它将使用该解码器对列表中的每个项目进行解码
我们将使用的解码器是Decode.field“price”Decode.float
,它将对象的“price”字段解码为float
我还将priceDecoder
的类型从Decoder String
更改为Decoder(List Float)
,因为价格是Float
,我们解码的是List
,而不仅仅是得到第一个或最后一个值或类似的东西。我想这就是你想要的,因为你说“至少还每个价格”
我们得到的priceDecoder
是:
priceDecoder : Decode.Decoder (List Float)
priceDecoder =
Decode.field "data" (Decode.list (Decode.field "price" Decode.float))
问题本身有几个问题。首先,您没有发布您收到的错误。我收到的错误如下:
The 2nd argument to `field` is not what I expect:
25| Decode.field "data" Decode.list
^^^^^^^^^^^
This `list` value is a:
Decode.Decoder a -> Decode.Decoder (List a)
But `field` needs the 2nd argument to be:
Decode.Decoder a
其次,您发布的JSON无效,数据应包含在引号中,属性和值应以:
分隔,而不是=
分隔
第三,priceDecoder
的类型似乎是错误的,因为JSON不包含String
数据,或者您没有指定要将Float
转换为String
。我假设该类型是错误的
因此,您(或至少我)得到的错误是,list
是一个函数Decoder a->Decoder(list a)
,而它只需要一个Decoder a
值。这是因为list
需要向它传递一个Decoder a
,它将使用该函数对列表中的每个项目进行解码
我们将使用的解码器是Decode.field“price”Decode.float
,它将对象的“price”字段解码为float
我还将priceDecoder
的类型从Decoder String
更改为Decoder(List Float)
,因为价格是Float
,我们解码的是List
,而不仅仅是得到第一个或最后一个值或类似的东西。我假设这就是你所想要的。”至少返回每个价格'
我们得到的priceDecoder
是:
priceDecoder : Decode.Decoder (List Float)
priceDecoder =
Decode.field "data" (Decode.list (Decode.field "price" Decode.float))