Skip to content

Instantly share code, notes, and snippets.

@statusfailed
Created November 27, 2015 09:28
Show Gist options
  • Save statusfailed/ac1ec651d827c719173c to your computer and use it in GitHub Desktop.
Save statusfailed/ac1ec651d827c719173c to your computer and use it in GitHub Desktop.
Example of JSON decoding in elm
import Html exposing (Html, text)
import Signal exposing (constant)
import Json.Decode as Json exposing ((:=))
type alias Person =
{ name : String
, age : Int
}
personDecoder : Json.Decoder Person
personDecoder =
Json.object2 Person
("name" := Json.string)
("age" := Json.int)
exampleString : String
exampleString =
"{\"name\": \"john smith\", \"age\": 20}"
main : Html
main =
case Json.decodeString personDecoder exampleString of
Err _ -> text "oops"
Ok v -> text (toString v)
@jenyckee
Copy link

import Json.Decode as Json exposing ((:=)) doesn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment