F# 使用嵌套列表为 Json 定义类型
F# Define Types for Json with nested list
跟进这个 的问题。
我的 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"]
}
]
},
{......,
....}]}}}
到目前为止我的代码:
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 // ?
|}
}
如何定义这些类型?下一步将是:
let jsonObject = JObject.Parse(graphResponse)
let frame =
jsonObject.["data"].["Shipments"].["Data"].Children()
|> Seq.map (fun jtok -> jtok.ToObject<ApiTypes>())
|> Frame.ofRecords
我可以在一个类型中定义所有内容,还是必须为 ShipmentContents 定义一个单独的类型?
非常感谢您的帮助!
Fiddle here(不起作用,但为了共享代码)
如果您想将所有内容集中在一种类型中,这应该可行:
type ApiTypes =
{
ItemId : Guid
CompletedDate : Nullable<DateTime>
Organization:
{|
OrganizationId : Guid
OrganizationName : string
|}
ShipmentContent :
{|
MaterialId : Guid
Quantity : Nullable<decimal>
Unit : string
Labels: string[]
|}[]
}
请注意,我已将列表定义为数组,因为数组是 JSON 支持的集合类型。
跟进这个
我的 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"]
}
]
},
{......,
....}]}}}
到目前为止我的代码:
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 // ?
|}
}
如何定义这些类型?下一步将是:
let jsonObject = JObject.Parse(graphResponse)
let frame =
jsonObject.["data"].["Shipments"].["Data"].Children()
|> Seq.map (fun jtok -> jtok.ToObject<ApiTypes>())
|> Frame.ofRecords
我可以在一个类型中定义所有内容,还是必须为 ShipmentContents 定义一个单独的类型?
非常感谢您的帮助!
Fiddle here(不起作用,但为了共享代码)
如果您想将所有内容集中在一种类型中,这应该可行:
type ApiTypes =
{
ItemId : Guid
CompletedDate : Nullable<DateTime>
Organization:
{|
OrganizationId : Guid
OrganizationName : string
|}
ShipmentContent :
{|
MaterialId : Guid
Quantity : Nullable<decimal>
Unit : string
Labels: string[]
|}[]
}
请注意,我已将列表定义为数组,因为数组是 JSON 支持的集合类型。