2
我這種情況解析嵌套的JSON在榆樹
-- this is in post.elm
type alias Model =
{ img : String
, text : String
, source : String
, date : String
, comments : Comments.Model
}
-- this is in comments.elm
type alias Model =
List Comment.Model
-- this is in comment.elm
type alias Model =
{ text : String
, date : String
}
我試圖解析這樣形成一個JSON
{
"posts": [{
"img": "img 1",
"text": "text 1",
"source": "source 1",
"date": "date 1",
"comments": [{
"text": "comment text 1 1",
"date": "comment date 1 1"
}]
}
}
這是我Decoder
decoder : Decoder Post.Model
decoder =
Decode.object5
Post.Model
("img" := Decode.string)
("text" := Decode.string)
("source" := Decode.string)
("date" := Decode.string)
("comments" := Decode.list Comments.Model)
decoderColl : Decoder Model
decoderColl =
Decode.object1
identity
("posts" := Decode.list decoder)
它不工作,我得到
Comments
不公開Model
。
你如何暴露type alias
?
如何爲我的示例設置Decoder
?
此非常感謝,你幫助了很多! –
你能看看這個問題嗎?我再堅持這一點,進一步對結果我希望http://stackoverflow.com/questions/35845299/structure-mismatch-while-parsing-json –
榆樹新人:小心,這個答案包含pre- 0.18語法。特別是,':='已經變成'field','objectX'變成'mapX'。請參見[在覈心中升級到0.18 /重新命名的函數](https://github.com/elm-lang/elm-platform/blob/master/upgrade-docs/0.18.md#renamed-functions-in-core) –