这是我的代码:
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