【发布时间】:2017-01-12 06:07:23
【问题描述】:
我最近尝试使用 Elm 的 Http 模块从服务器获取数据,但我一直坚持将 json 解码为 Elm 中的自定义类型。
我的 JSON 看起来像这样:
[{
"id": 1,
"name": "John",
"address": {
"city": "London",
"street": "A Street",
"id": 1
}
},
{
"id": 2,
"name": "Bob",
"address": {
"city": "New York",
"street": "Another Street",
"id": 1
}
}]
应该解码为:
type alias Person =
{
id : Int,
name: String,
address: Address
}
type alias Address =
{
id: Int,
city: String,
street: String
}
到目前为止我发现我需要编写一个解码器函数:
personDecoder: Decoder Person
personDecoder =
object2 Person
("id" := int)
("name" := string)
对于前两个属性,但是我如何集成嵌套的地址属性以及如何结合它来解码列表?
【问题讨论】: