jsonKeys.fsx:
#r "System.Web.Extensions.dll"
open System
open System.IO
open System.Collections.Generic
open System.Web.Script.Serialization
let _ =
let jsonStr = File.ReadAllText "json.txt"
let jss = new JavaScriptSerializer();
let dic = jss.DeserializeObject(jsonStr) :?> Dictionary<string, obj>
let keyList = dic.Keys |> Seq.toList
printfn "%A" keyList
DEMO
>fsi jsonKeys.fsx
["id"; "name"; "first_name"; "last_name"; "link"; "username"; "birthday"; "work";
"education"; "gender"; "locale"; "updated_time"; "type"]
ДОБАВИТЬ СЛУЧАЙ ВХОДА, КАК
{"data":[{"id":"902395","name":"Thomas Girba"},{"id":"194589","name":"Durand Gure"},..]}
let listToTuple [x;y] = (string x, string y)
let _ =
let jsonStr = File.ReadAllText "json2.txt"
let jss = new JavaScriptSerializer();
let dic = jss.DeserializeObject(jsonStr) :?> Dictionary<string, obj>
let data = dic.["data"] :?> obj []
(*
let result =
data
|> Array.map (fun x -> (x :?> Dictionary<string,obj>).Keys |> Seq.toList |> listToTuple)
printfn "%A" result //[|("id", "name"); ("id", "name")|]
*)
let result =
data
|> Array.map (fun x ->
let d = x :?> Dictionary<string,obj>
string d.["id"], string d.["name"] //value
)
printfn "%A" result //[|("902395", "Thomas Girba"); ("194589", "Durand Gure")|]
open Microsoft.FSharp.Reflection
let arrayToTuple (ar: 'a array) =
let len = Array.length ar
let ty = FSharpType.MakeTupleType(Array.create len typeof<'a>)
FSharpValue.MakeTuple(ar, ty)