Created
November 27, 2015 09:28
-
-
Save statusfailed/ac1ec651d827c719173c to your computer and use it in GitHub Desktop.
Example of JSON decoding in elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import Json.Decode as Json exposing ((:=))
doesn't work