Created
December 1, 2014 01:11
-
-
Save jeremyabbott/07ef2ad70961ad5431d2 to your computer and use it in GitHub Desktop.
F# Deserialize
This file contains hidden or 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
open System.Net.Http | |
open System.Net.Http.Formatting | |
open Newtonsoft; | |
// Learn more about F# at http://fsharp.net | |
// See the 'F# Tutorial' project for more help. | |
[<CLIMutable>] | |
type Movie = { | |
ID : int; | |
Name : string } | |
let getMovies () = | |
let client = new HttpClient() | |
async { | |
let! response = client.GetAsync("http://nlglff.org/api/films") |> Async.AwaitTask | |
response.EnsureSuccessStatusCode () |> ignore | |
// has to be done in two steps | |
// for some reason ReadAsAsync doesn't deserialize correctly. | |
let! content = response.Content.ReadAsStringAsync() |> Async.AwaitTask | |
let result = Json.JsonConvert.DeserializeObject<Movie list>(content) | |
return result | |
} | |
[<EntryPoint>] | |
let main argv = | |
let movies = Async.RunSynchronously <| getMovies () | |
//printfn "%s" movies | |
movies |> List.iter (fun m -> printfn "Name: %s; ID: %i" m.Name m.ID) | |
0 // return an integer exit code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment