This is my first question about SO ... so don't judge strictly =)
Usually all my techout questions are in chat rooms (believe me, many of them =)).
Recently, we are talking about RosettaCode . And I would like to add some of the task codes to F #
One of them is JSON .
One possible solution is to use "F # Data: JSON Parser". So my question is related to this.
This code works well:
open FSharp.Data open FSharp.Data.JsonExtensions type Person = {ID: int; Name:string} let json = """[ { "ID": 1, "Name": "First" }, { "ID": 2, "Name": "Second" }]""" json |> printfn "%s" match JsonValue.Parse(json) with | JsonValue.Array(x) -> x |> Array.map(fun x -> {ID = System.Int32.Parse((x?ID).ToString()); Name = (string x?Name)}) | _ -> failwith "fail json" |> Array.iter(fun x -> printfn "%i %s" x.ID x.Name)
Print
[ { "ID": 1, "Name": "First" }, { "ID": 2, "Name": "Second" }] 1 "First" 2 "Second"
But he
{ID = System.Int32.Parse((x?ID).ToString()); Name = (string x?Name)}
It doesn’t look very good.
This is what I read about JsonExtensions,
but when i use
{ID = (x?ID.AsInteger()) ; Name = (x?Name.AsString()) }
I get compilation errors:
Field, Constructor, or "AsInteger" Not Defined
Field, Constructor, or "AsString" Not Defined
Strange, the fact is that I see accessibility through "open FSharp.Data.JsonExtensions"

So the question is: how to use JsonExtensions?
source share