我如何将此属性添加到JsonConvert

本文关键字:添加 JsonConvert 属性 | 更新日期: 2023-09-27 18:15:14

这是我的代码:

var json = JsonConvert.SerializeObject(myObject, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });

如何添加此属性?

new IsoDateTimeConverter()

因为它以我不喜欢的方式打印DateTime。通常是:

var json = JsonConvert.SerializeObject(myObject, new IsoDateTimeConverter());

我如何将此属性添加到JsonConvert

将转换器添加到JsonSerializerSettings对象上的Converters集合

JsonSerializerSettings settings = new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.Objects,
    Formatting = Formatting.Indented,
    Converters = new List<JsonConverter> { new IsoDateTimeConverter() }
};
var json = JsonConvert.SerializeObject(myObject, settings);

小提琴:https://dotnetfiddle.net/Kxprpr