使用FSHARP.DATA的JSONPROVIDER进行Binance-Requeast

发布于 2025-01-22 02:57:43 字数 1650 浏览 4 评论 0原文

我正在尝试使用 binance-connector for .net。在f#-Examples中,我们有以下内容:

let f = 
    let loggerFactory = LoggerFactory.Create(fun (builder:ILoggingBuilder) -> 
        builder.AddConsole() |> ignore
    )
    let logger = loggerFactory.CreateLogger()

    let loggingHandler = new BinanceLoggingHandler(logger)
    let httpClient = new HttpClient(loggingHandler)
    
    let market = new Market(httpClient)
    
    let result = market.TwentyFourHrTickerPriceChangeStatistics() |> Async.AwaitTask |> Async.RunSynchronously

    0

结果是一个字符串,看起来像这样:

{"symbol":"ETHBTC","priceChange":"0.00013400","priceChangePercent":"0.179","weightedAvgPrice":"0.07444089","prevClosePrice":"0.07467300","lastPrice":"0.07480700","lastQty":"0.04640000","bidPrice":"0.07480600","bidQty":"6.01380000","askPrice":"0.07480700","askQty":"48.54320000","openPrice":"0.07467300","highPrice":"0.07531600","lowPrice":"0.07357000","volume":"80296.33090000","quoteVolume":"5977.33041290","openTime":1650281947747,"closeTime":1650368347747,"firstId":335177449,"lastId":335313233,"count":135785}

这是按预期工作的,但是我当然想处理结果。 因此,我试图与JSONPROVIDER进行应对:

   type Simple = JsonProvider<result>

这是由于某种原因行不通的。该错误存在,并说未定义构造函数或值(FS0039) JSONPROVIDER的文档中的一个示例如下:

type Simple = JsonProvider<""" { "name":"John", "age":94 } """>
let simple = Simple.Parse(""" { "name":"Tomas", "age":4 } """)
simple.Age
simple.Name

如何正确地将JSON施加到一种类型中?

此致

I'm trying to work with the Binance-Connector for .NET. In the F#-Examples, we have the following:

let f = 
    let loggerFactory = LoggerFactory.Create(fun (builder:ILoggingBuilder) -> 
        builder.AddConsole() |> ignore
    )
    let logger = loggerFactory.CreateLogger()

    let loggingHandler = new BinanceLoggingHandler(logger)
    let httpClient = new HttpClient(loggingHandler)
    
    let market = new Market(httpClient)
    
    let result = market.TwentyFourHrTickerPriceChangeStatistics() |> Async.AwaitTask |> Async.RunSynchronously

    0

result is a string and looks something like this:

{"symbol":"ETHBTC","priceChange":"0.00013400","priceChangePercent":"0.179","weightedAvgPrice":"0.07444089","prevClosePrice":"0.07467300","lastPrice":"0.07480700","lastQty":"0.04640000","bidPrice":"0.07480600","bidQty":"6.01380000","askPrice":"0.07480700","askQty":"48.54320000","openPrice":"0.07467300","highPrice":"0.07531600","lowPrice":"0.07357000","volume":"80296.33090000","quoteVolume":"5977.33041290","openTime":1650281947747,"closeTime":1650368347747,"firstId":335177449,"lastId":335313233,"count":135785}

This works as intended, but of course I want to work with the result.
So I tried to deserialize it with the JsonProvider:

   type Simple = JsonProvider<result>

Which for some reason doesn't work. The error resides with and it says that the Constructor or value is not defined (FS0039)
A sample in the docs for JsonProvider is given as follows:

type Simple = JsonProvider<""" { "name":"John", "age":94 } """>
let simple = Simple.Parse(""" { "name":"Tomas", "age":4 } """)
simple.Age
simple.Name

How can I correctly cast the json to a type?

Best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2025-01-29 02:57:43

jsonprovider&lt; ...&gt;采用静态字符串参数,该参数是示例字符串或示例文件(本地或在线访问),以便提供者从中推断出类型。

因此,在您的情况下,这将是:

let [<Literal>] BinanceSample = """ [{"symbol":"ETHBTC","priceChange":"-0.00163000"}] """

type Binance = JsonProvider<BinanceSample>

然后,您应该能够使用:

let parsed = Binance.Parse(result)

PS:尝试提供尽可能完整的JSON样本。

JsonProvider<...> takes a static string parameter which is either a sample string or a sample file (local or online accessible) in order to let the provider infer the types from it.

So in your case it would be:

let [<Literal>] BinanceSample = """ [{"symbol":"ETHBTC","priceChange":"-0.00163000"}] """

type Binance = JsonProvider<BinanceSample>

Then you should be able to parse the JSON with:

let parsed = Binance.Parse(result)

PS: try to provide a JSON sample that is as complete as possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文