简体   繁体   English

F# 使用嵌套列表定义 Json 的类型

[英]F# Define Types for Json with nested list

Follow up question to this question, to parse json to deedle dataframe .跟进这个问题,解析 json 到 deedle dataframe

My json includes some list with nested lists:我的 json 包括一些带有嵌套列表的列表:

apiResponse = {"data":
                    {"Shipments":
                               {"ErrorMessage":null,
                                 "Success":true,
                                 "ValidationResult":null,
                                 "TotalCount":494,
                                 "Data":[
                                         {
                                         "CompletedDate":"2021-04-12T10:13:13.436Z",
                                          "ItemId":"dd4520bb-aa0a-...",
                                          "Organization":{
                                                      "OrganizationId":"cac43a32-1f08-...",
                                                      "OrganizationName":"XXX"
                                                      },
                                          "ShipmentContent" : [
                                                           {
                                                             "MaterialId": "cac43a32-1f08-..",
                                                             "unit": "kg",
                                                             "quantity": 2.0, 
                                                             "labels" : ["A1", "A2", "A3"]
                                                            }, 
                                                            {
                                                             "MaterialId": "cac43333-1f08-",
                                                             "unit": "t",
                                                             "quantity": 0.4, 
                                                             "labels" : ["B1", "B2", "B3"]
                                                             }
                                                             ] 
                                     },
                                     {......,
                                     ....}]}}}

My code so far:到目前为止我的代码:

type ApiTypes = 
  {
    ItemId : Guid
    CompletedDate : Nullable<DateTime> 
    Organization: 
      {|
        OrganizationId : Guid
        OrganizationName : string
       |}
    ShipmentContent : // ? How to define the List
      {|
        MaterialId : Guid 
        Quantity : Nullable<decimal> 
        Unit : string
        Labels: List // ?
              
      |}
  }

How can I define these types?如何定义这些类型? The next step would be:下一步将是:

let jsonObject = JObject.Parse(graphResponse)

let frame =
  jsonObject.["data"].["Shipments"].["Data"].Children()
      |> Seq.map (fun jtok -> jtok.ToObject<ApiTypes>())
      |> Frame.ofRecords

Can I define all in one type, or do I have to define a seperate type for ShipmentContents?我可以在一种类型中定义所有内容,还是必须为 ShipmentContents 定义单独的类型?

I realy apreciate your help!我真的很感谢你的帮助!

Fiddle here (doesn't work, but for sharing the code) Fiddle here (不起作用,但用于共享代码)

This should work if you want to have it all in one type:如果您想将所有内容都集中在一种类型中,这应该可以工作:

type ApiTypes = 
  {
    ItemId : Guid
    CompletedDate : Nullable<DateTime> 
    Organization: 
      {|
        OrganizationId : Guid
        OrganizationName : string
      |}
    ShipmentContent :
      {|
        MaterialId : Guid 
        Quantity : Nullable<decimal> 
        Unit : string
        Labels: string[]
      |}[]
  }

Note that I've defined the lists as arrays, since arrays are the collection type supported by JSON.请注意,我已将列表定义为 arrays,因为 arrays 是 JSON 支持的集合类型。

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

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