我正在发现Json文件,我想创建一个F#代码,它将读取以下Json文件(从Facebook连接中提取)并将数据放入F#列表中,如下例所示:["id";"Name";"first_name";"last_name";"link";"username";"birthday";"gender"]
。你有什么想法 ?我不是这方面的专家,如果您对此有所了解,对我来说非常有帮助
Json文件:
{
"id": "6234306",
"name": "Katter Maxime",
"first_name": "Katter",
"last_name": "Maxime",
"link": "http://www.facebook.com/katter.maxime",
"username": "katter.maxime",
"birthday": "11/10/1982",
"work": [
{
"employer": {
"id": "10164958321",
"name": "Bedur"
},
"position": {
"id": "14014810602",
"name": "student"
},
"start_date": "0000-00",
"end_date": "0000-00"
}
],
"education": [
{
"school": {
"id": "107990348",
"name": "AAAAAAAA"
},
"year": {
"id": "1406175699",
"name": "2007"
},
"type": "Graduate School"
}
],
"gender": "male",
"locale": "en_US",
"updated_time": "2011-11-03T17:31:04+0000",
"type": "user"
}
答案 0 :(得分:4)
使用Json.NET
[ for KeyValue(key, _) in (JObject.Parse(json) :> IDictionary<_,_>) -> key ]
答案 1 :(得分:3)
<强> 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
<强>样本强>
>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)