简体   繁体   中英

Creating a value of a type f#

  type ExcludeToIncludeCombinationsWhenMergingJson = JsonProvider<"""
  {
    "data": {
      "excludeToIncludeList": [
        {
          "budgetType": "bottomUp",
          "budgetYear": 2019,
          "outletId": 9223372036854775806
        },
        {
          "budgetType": "bottomUp",
          "budgetYear": 2019
        }
      ]
    }
  }
  """,SampleIsList=true>

I am new to f#.I have a type like this. How can I create A value of this type? I am really struggling with this.

I tried this

let r = ExcludeToIncludeCombinationsWhenMergingJson.Root(ExcludeToIncludeCombinationsWhenMergingJson.Data(ExcludeToIncludeCombinationsWhenMergingJson.ExcludeToIncludeList([{BudgetType = "bottomup"; BudgetYear = 2019; OutletId = 5;}]))) 

It does not work. Any help will be appreciated. Any kind of document that I can follow

To create a value of the type ExcludeToIncludeCombinationsWhenMergingJson you need to call the method Parse with input is a real json string you want to parse, like this:

let jsonString = ... // read from file or network
let r = ExcludeToIncludeCombinationsWhenMergingJson.Parse jsonString
// now use r
r.Data.ExcludeToIncludeList ...

Notice that the Parse method will throw an exception when the jsonString is invalid (not have correct structure/format as specified in the sample string).

To parse the sample json string itself, you can use the method GetSample or GetSamples (the later is available only when the sample json string is a list).

Also notice that your posted sample json string is not a list, so please remove the ,SampleIsList=true

For more details, please refer to the official document of Json Type Provider: http://fsharp.github.io/FSharp.Data/library/JsonProvider.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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