和F#Type提供商一起玩,并没有太远。我有以下json文件(称为PriceDemand.json):
[
{
"intervalDate": "2018-01-22T00:00:00+11:00",
"regionId": "NSW1",
"rrp": 114.17,
"totalDemand": 12338.04
},
{
"intervalDate": "2018-01-22T00:00:00+11:00",
"regionId": "NSW1",
"rrp": 113.41,
"totalDemand": 12334.98
}
]
我编写了以下代码来处理它:
open FSharp.Data
open System
type PriceDemand = JsonProvider<"PriceDemand.json">
let data = PriceDemand.Parse("PriceDemand.json")
[<EntryPoint>]
let main argv =
data |> Seq.iter (fun v -> printf "%s" v.RegionId)
Console.ReadLine() |> ignore
0 // return an integer exit code
我有PriceDemand
类型的intellisense,但以下TypeInitializationExceptionexception
被抛出:
从字符0开始的无效JSON, 片段= ---- PriceDemand ----- json = ------ PriceDemand.json
我有什么想法我做错了吗?
你在调用.Parse
,你应该打电话给.Load
。字符串"PriceDemand.json"
被解析为JSON,这是无效的。如果您将调用更改为let data = PriceDemand.Load("PriceDemand.json")
,它应该可以正常工作。