简体   繁体   English

将Json信息转换为F#列表

[英]Convert Json information into a F# list

I am discovering Json files and I would like to create a F# code that would read the following Json file (extracted from Facebook connect) and put the datas in F# lists like in this case: ["id";"Name";"first_name";"last_name";"link";"username";"birthday";"gender"] . 我正在发现Json文件,我想创建一个F#代码,该代码将读取以下Json文件(从Facebook connect中提取),并将数据放入F#列表中,例如在这种情况下: ["id";"Name";"first_name";"last_name";"link";"username";"birthday";"gender"] Do you have any ideas ? 你有什么想法 ? I am not an expert in this and it would be very helpful for me if you have an idea about this 我不是这方面的专家,如果您对此有所了解,这对我会非常有帮助

The Json file : 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"
}

使用Json.NET

[ for KeyValue(key, _) in (JObject.Parse(json) :> IDictionary<_,_>) -> key ]

jsonKeys.fsx: 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"]

ADD CASE OF INPUT LIKE 添加类似输入的案例

{"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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM