我有一组数据,使用http://jsonapi.org/格式:
{
"data": [
{
"type": "prospect",
"id": "1",
"attributes": {
"provider_user_id": "1",
"provider": "facebook",
"name": "Julia",
"invitation_id": 25
}
},
{
"type": "prospect",
"id": "2",
"attributes": {
"provider_user_id": "2",
"provider": "facebook",
"name": "Sam",
"invitation_id": 23
}
}
]
}
我有这样的模型:
type alias Model = {
id: Int,
invitation: Int,
name: String,
provider: String,
provider_user_id: Int
}
type alias Collection = List Model
我想将JSON解码成Collection,但不知道该怎么做。
fetchAll: Effects Actions.Action
fetchAll =
Http.get decoder (Http.url prospectsUrl [])
|> Task.toResult
|> Task.map Actions.FetchSuccess
|> Effects.task
decoder: Json.Decode.Decoder Collection
decoder =
?
我该如何实现解码器?谢谢
Json.Decode.decodeString
或decodeValue
。 - mgold:=
变成了Decode.field
。我已经更新了示例。 - mgolddecoderColl = Decode.map identity (Decode.field "data" (Decode.list decoder))
。 - Eric Walker