【发布时间】:2020-04-06 04:25:07
【问题描述】:
考虑一个包含如下列表值的 F# 记录:
type MyRecord = {
Name: string
SomeList: string list
}
当 JSON 不包含记录的列表值 Values 的属性时,使用 Netwonsoft.Json.JsonConvert 将 JSON 反序列化到此记录将导致反序列化的记录具有列表的 null 值而不是空值列出[]。
也就是说,
open Newtonsoft.Json
JsonConvert.DeserializeObject<MyRecord>("""{ "Name": "Some name"}""" ) |> printfn "%A"
// Gives: { Name = "Some name"; SomeList = null; }
如何使用Netwonsoft.Json 反序列化,以便将列表初始化为空列表?例如:
{ Name = "Some name"; SomeList = []; }
【问题讨论】: