Last active
May 1, 2019 05:36
-
-
Save yang-wei/2d1c33b9c114fcd652ff to your computer and use it in GitHub Desktop.
Json.Decode example
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 Graphics.Element exposing (Element, show) | |
import Task exposing (Task, andThen) | |
import Json.Decode exposing (Decoder, int, string, object3, (:=), at, keyValuePairs) | |
import Http | |
type alias Languages = | |
List (String, Int) | |
mailbox = | |
Signal.mailbox [] | |
-- VIEW | |
main : Signal Element | |
main = | |
Signal.map show mailbox.signal | |
-- TASK | |
fetchApi = | |
Http.get decoder api | |
handleResponse data = | |
Signal.send mailbox.address data | |
decoder : Decoder (List (String, Int)) | |
decoder = | |
keyValuePairs int | |
port run : Task Http.Error () | |
port run = | |
fetchApi `andThen` handleResponse | |
api = | |
"https://api.github.com/repos/elm-lang/elm-lang.org/languages" |
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 Graphics.Element exposing (Element, show) | |
import Task exposing (Task, andThen) | |
import Json.Decode exposing (Decoder, int, string, list, at, (:=), object1) | |
import Dict exposing (Dict) | |
import Http | |
mailbox = | |
Signal.mailbox [] | |
-- VIEW | |
main : Signal Element | |
main = | |
Signal.map show mailbox.signal | |
-- TASK | |
fetchApi = | |
Http.get decoder api | |
handleResponse data = | |
Signal.send mailbox.address data | |
fullNameDecoder : Decoder String | |
fullNameDecoder = | |
object1 identity ("full_name" := string) | |
decoder = | |
at ["items"] (list fullNameDecoder) | |
port run : Task Http.Error () | |
port run = | |
fetchApi `andThen` handleResponse | |
api = | |
"https://api.github.com/search/repositories?q=language:elm&sort=starts&language=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 Graphics.Element exposing (Element, show) | |
import Task exposing (Task, andThen) | |
import Json.Decode exposing (Decoder, int, string, object3, (:=)) | |
import Http | |
type alias RepoRecord = | |
{ name : String | |
, description : String | |
, watchers_count : Int | |
} | |
mailbox = | |
Signal.mailbox (RepoRecord "" "" 0) | |
-- VIEW | |
main : Signal Element | |
main = | |
Signal.map show mailbox.signal | |
-- TASK | |
fetchApi = | |
Http.get repoRecordDecoder api | |
handleResponse data = | |
Signal.send mailbox.address data | |
repoRecordDecoder : Decoder RepoRecord | |
repoRecordDecoder = | |
object3 RepoRecord | |
("name" := string) | |
("description" := string) | |
("watchers_count" := int) | |
port run : Task Http.Error () | |
port run = | |
fetchApi `andThen` handleResponse | |
api = | |
"https://api.github.com/repos/elm-lang/core" |
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 Graphics.Element exposing (Element, show) | |
import Task exposing (Task, andThen) | |
import Json.Decode exposing (Decoder, int, string, object3, (:=)) | |
import Http | |
type alias RepoTuple = ( String, String, Int ) | |
mailbox = | |
Signal.mailbox ("", "", 0) | |
-- VIEW | |
main : Signal Element | |
main = | |
Signal.map show mailbox.signal | |
-- TASK | |
fetchApi = | |
Http.get repoTupleDecoder api | |
handleResponse data = | |
Signal.send mailbox.address data | |
-- decoder changes depends on our data type | |
repoTupleDecoder : Decoder RepoTuple | |
repoTupleDecoder = | |
object3 (,,) | |
("name" := string) | |
("description" := string) | |
("watchers_count" := int) | |
port run : Task Http.Error () | |
port run = | |
fetchApi `andThen` handleResponse | |
api = | |
"https://api.github.com/repos/elm-lang/core" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
whats your elm-package look like?
what version of elm are you running?