我如何将此属性添加到JsonConvert



这是我的代码:

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

如何添加此属性?

new IsoDateTimeConverter()

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

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

将转换器添加到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

相关内容

  • 没有找到相关文章

最新更新