解析JSON时结构不匹配 [英] Structure mismatch while parsing JSON

查看:145
本文介绍了解析JSON时结构不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析JSON文件并将其显示给用户,这里是简化版本

I am trying to parse a JSON file and show it to the user, here a simplified version

{
  "posts": [{ 
    // some properties 
    comments: {
      // some more properties
    }
}

这是我的代码,我知道很多时候我不确定要删除什么来隔离问题,每次尝试时,我都会遇到另一个错误,但似乎无济于事.

This is my code, I know it's a lot I'm not really sure what to remove to isolate the problem, every time I try I get a different error that seems to lead me nowhere.

type Action
  = NoOp
  | GetPosts
  | ShowPosts (Maybe { posts : List Post.Model })


init =
  ( { posts = Nothing }, Effects.none )


update action model =
  case action of
    NoOp ->
      ( model, Effects.none )

    GetPosts ->
      ( { model | posts = Nothing }, getPosts )

    ShowPosts maybePosts ->
      ( { model | posts = maybePosts }, Effects.none )


view address model =
  div
    []
    [ button [ onClick address GetPosts ] [ text "Click to get posts!" ]
    , viewPosts model.posts
    ]


viewPosts maybePosts =
  case maybePosts of
    Nothing ->
      div [] [ text "No posts to display. Try clicking the button" ]

    Just posts ->
      ul [] (List.map Post.view posts)



-- This is the key to map the result of the HTTP GET to an Action
-- Note: Task.toMaybe swallows any HTTP or JSON decoding errors


getPosts : Effects Action
getPosts =
  Http.get decoderColl "./posts.json"
    |> Task.toMaybe
    |> Task.map ShowPosts
    |> Effects.task


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


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


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


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

我从编译器收到此错误

函数start期望参数为:

{ ...
, view :
      Signal.Address Action
      -> { posts : Maybe { posts : List Post.Model } }
      -> Html
}

但这是

{ ...
, view :
      Signal.Address Action -> { posts : Maybe (List Post.Model) } -> Html
}

view的定义中,我无法理解多余的{ posts : Maybe来自何处.

I can't understand where that extra { posts : Maybe is coming from in the definition of view.

具有一些额外背景知识的上一个问题:在Elm中解析嵌套的JSON

Previous question with some extra background: Parsing nested JSON in Elm

更新:

在elm社区google组中得到了答案,这是要旨 https://gist.github. com/rundis/23d7ef6ea42842e6f527

Got an answer in the elm community google group, here's the gist https://gist.github.com/rundis/23d7ef6ea42842e6f527

推荐答案

我认为ShowPosts的定义正在妨碍您.你有这个:

I think the definition of ShowPosts is getting in the way. You have this:

ShowPosts (Maybe { posts : List Post.Model })

但是可能应该是这样:

ShowPosts (Maybe (List Post.Model))

进行此更改将导致您不得不更新其他一些位置,但是请遵循编译器消息,它应将您带到正确的位置.

Making that change will cause you to have to update a few other places, but follow the compiler messages and it should lead you to the right place.

需要更新的地方之一是getPosts,您需要在其中删除该包装对象中的帖子列表.那应该像这样简单:

One of the places that will need updating is getPosts, where you'll need to take the list of posts out of that wrapper object. That should be as simple as this:

|> Task.map (ShowPosts << .posts)

这篇关于解析JSON时结构不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆