f#的JSON类型提供程序:获取行/列名称

时间:2021-02-12 01:34:10

I am using trying to use JSON Provider from FSharp.Data http://fsharp.github.io/FSharp.Data/library/JsonProvider.html

我使用的是FSharp的JSON提供程序。http://fsharp.github.io/FSharp.Data/library/JsonProvider.html的数据

type BookOrder = JsonProvider<"../Documents/sample.json">

let commandType = "returnOrderBook" 
let currencyPair = "BTC_NXT"
let depth = string 3

let polReq = "https://poloniex.com/public?" + "command=" + commandType + "&currencyPair=" + currencyPair + "&depth="
+ depth

let BookOrderNow = BookOrder.Load(polReq)

 val BookOrderNow : JsonProvider<...>.Root =   {   "asks": [   [   "0.00001605",   14636.67789781   ],   [   "0.00001606",   73739.31116785   ],   [   "0.00001607",   1342158.7102721   ]   ],   "bids": [   [   "0.00001593",   17805.17982312   ],   [   "0.00001591",   71659.725   ],   [   "0.00001590",   67600.19748428   ]   ],   "isFrozen": "0",   "seq": 29534867  }

val BookOrderNow:JsonProvider <…>。根= {“问”:[[“0.00001605”,14636.67789781],[“0.00001606”,73739.31116785],[“0.00001607”,1342158.7102721]],“投标”:[[“0.00001593”,17805.17982312],[“0.00001591”,71659.725],[“0.00001590”,67600.19748428]],“isFrozen”:“0”,“seq”:29534867 }

BookOrderNow.Bids

 

 

val it : decimal [] [] =   [|[|0.00001593M; 17805.17982312M|]; [|0.00001591M; 71659.725M|];   [|0.00001590M; 67600.19748428M|]|]

[] = [|] [|0.00001593M;17805.17982312 |];[| 0.00001591米;71659.725 |];[| 0.00001590米;67600.19748428 | |)

is there a method to get as an output

有什么方法可以得到输出吗?

["asks";"bids";"isFrozen";"seq"]

(“问”;“投标”;“isFrozen”;“seq”)

?

吗?

and if i did get from some function/method "bids" in some variable varname is there a function like (in this particular case)

如果我确实从变量varname中的函数/方法"bid "中得到一个函数(在本例中)

GetJsonContent: JsonProvider<...>.Root -> string -> ???
GetJsonContent BookOrderNow varname

and return the value of BookOrderNow.Bids ?

然后返回BookOrderNow的值。报价吗?

I mean, if we know IN ADVANCE the contents of the JSON file, we don't need to do this, but if we don't, ... ? Ultimately i want to put the data in Deedle Frame / Series objects

我的意思是,如果我们事先知道JSON文件的内容,我们不需要这样做,但是如果我们不知道,……吗?最终我想把数据放到Deedle框架/系列对象中

Thank you for any advice. This is the first time i'm trying to use this library seriously

谢谢你的建议。这是我第一次认真地使用这个图书馆

1 个解决方案

#1


1  

the answer was given to me by @FoggyFinder

答案是@FoggyFinder给我的

BookOrderNow.JsonValue.Properties()
   |> Seq.map fst
   |> printfn "%A" //=> seq ["asks"; "bids"; "isFrozen"; "seq"]

let getJsonContent (x:BookOrder.Root) varname =
    x.JsonValue.GetProperty varname
    |> JsonExtensions.AsArray
    |> Array.map
        (JsonExtensions.AsArray >> Array.map (JsonExtensions.AsDecimal))

However, as per the comment from @Matiasd ,

然而,根据@Matiasd的评论,

"Type providers are best suited to situations where the schema is stable at runtime and during the lifetime of compiled code."

“类型提供程序最适合于模式在运行时和编译代码的生命周期中是稳定的情况。”

Thank you to both of you

谢谢你们俩

#1


1  

the answer was given to me by @FoggyFinder

答案是@FoggyFinder给我的

BookOrderNow.JsonValue.Properties()
   |> Seq.map fst
   |> printfn "%A" //=> seq ["asks"; "bids"; "isFrozen"; "seq"]

let getJsonContent (x:BookOrder.Root) varname =
    x.JsonValue.GetProperty varname
    |> JsonExtensions.AsArray
    |> Array.map
        (JsonExtensions.AsArray >> Array.map (JsonExtensions.AsDecimal))

However, as per the comment from @Matiasd ,

然而,根据@Matiasd的评论,

"Type providers are best suited to situations where the schema is stable at runtime and during the lifetime of compiled code."

“类型提供程序最适合于模式在运行时和编译代码的生命周期中是稳定的情况。”

Thank you to both of you

谢谢你们俩