Parsing nested JSON in Elm

痴心易碎 提交于 2019-12-06 04:42:16

问题


I have this situation

-- 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
  }

I am trying to parse a JSON so formed

{
  "posts": [{
    "img": "img 1",
    "text": "text 1",
    "source": "source 1",
    "date": "date 1",
    "comments": [{
      "text": "comment text 1 1",
      "date": "comment date 1 1"
    }]
  }
}

This is my 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)

It does not work, I am getting

Comments does not expose Model.

How do you expose a type alias?

How do I set up Decoder for my example?


回答1:


Edit: Updated to Elm 0.18

To expose Comments.Model, make sure your Comments.elm file exposes all types and functions like this:

module Comments exposing (..)

Or, you can expose a subset of types and functions like this:

module Comments exposing (Model)

There are a few problems with your decoders. First off, to match your JSON, you're going to need a record type alias that exposes a single posts list Posts.

type alias PostListContainerModel =
  { posts : List Post.Model }

You are missing a decoder for comments. That will look like this:

commentDecoder : Decoder Comment.Model
commentDecoder =
  Decode.map2
    Comment.Model
    (Decode.field "text" Decode.string)
    (Decode.field "date" Decode.string)

I'm going to rename your decoder function to postDecoder to avoid ambiguity. Now you can fix the comments decoder line by using the new commentDecoder.

postDecoder : Decoder Post.Model
postDecoder =
  Decode.map5
    Post.Model
    (Decode.field "img" Decode.string)
    (Decode.field "text" Decode.string)
    (Decode.field "source" Decode.string)
    (Decode.field "date" Decode.string)
    (Decode.field "comments" (Decode.list commentDecoder))

Lastly, we can fix decoderColl by using that posts wrapper type (PostListContainerModel) we created earlier:

decoderColl : Decoder PostListContainerModel
decoderColl =
  Decode.map
    PostListContainerModel
    (Decode.field "posts" (Decode.list postDecoder))


来源:https://stackoverflow.com/questions/35801776/parsing-nested-json-in-elm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!