I'm attempting to write a simple program that will show me the latest posts on the r/programming sub-reddit (https://www.reddit.com/r/programming.json). As you can see if you click on that link the JSON response is really nasty which is why I'm having a great deal of problems trying to decode the response.
Below is the best SSCCE that I could come up with to reproduce the error. You should be able to paste it into http://elm-lang.org/try and see the error I'm getting. import Html exposing (..) import Html.App as App import Html.Attributes exposing (..) import Html.Events exposing (..) import Http import Json.Decode as Json exposing (Decoder, object1, object2, string, list, (:=)) import Task main = App.program { init = init , view = view , update = update , subscriptions = (\_ -> Sub.none) } -- MODEL type alias Model = { redditPosts : List RedditPostContent , error : Maybe String } type alias RedditJsonResponse = { kind : String , data : { children : List { kind : String , data : { title : String } } } } type alias RedditPostContent = { title : String } init : (Model, Cmd Msg) init = ( Model [] Nothing , getSubRedditContent ) -- UPDATE type Msg = FetchSucceed RedditJsonResponse | FetchFail Http.Error update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of FetchSucceed redditFetchResponse -> { model | redditPosts = List.map (\post -> post.data) redditFetchResponse.data.children } ! [] FetchFail httpError -> let error = case httpError of Http.UnexpectedPayload err -> "Unexpected Payload! " ++ err otherwise -> "something else bad happened" in {model | error = Just error} ! [] -- VIEW view : Model -> Html Msg view model = div [] [ h2 [] [Maybe.withDefault "No Error" model.error |> text] , h3 [] [ text "Reddit posts" , redditPostList model.redditPosts ] ] redditPostList : List RedditPostContent -> Html Msg redditPostList redditPosts = ul [] <| List.map (\redditPost -> li [] [ text redditPost.title ]) redditPosts -- HTTP getSubRedditContent : Cmd Msg getSubRedditContent = Task.perform FetchFail FetchSucceed <| Http.get redditJsonResponseDecoder "https://www.reddit.com/r/programming.json" redditJsonResponseDecoder : Decoder RedditJsonResponse redditJsonResponseDecoder = object2 RedditJsonResponse ("kind" := string) ("data" := object1 (\children -> { children = children }) ("children" := object2 (\kind data -> { kind = kind, data = data }) ("kind" := string) ("data" := object1 (\title -> { title = title }) ("title" := string) ) |> Json.list ) ) The error isn't terribly helpful except for the part where it says Expecting a List at _.data but instead got: {"modhash":"","children":[...] It's somewhat cryptic but I gather that somewhere I told it that `data` was an array but the actual JSON response had `data` not being an array. I can't find where I actually say data is an array. Any help would really be appreciated. -- You received this message because you are subscribed to the Google Groups "Elm Discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
