这是我的代码:
var json = JsonConvert.SerializeObject(myObject, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
我如何添加此属性?
new IsoDateTimeConverter()
因为它以我不喜欢的方式打印DateTime。通常我会这样做:
var json = JsonConvert.SerializeObject(myObject, new IsoDateTimeConverter());
答案 0 :(得分:1)
将转换器添加到JsonSerializerSettings
对象上的Converters
集合。
JsonSerializerSettings settings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
Formatting = Formatting.Indented,
Converters = new List<JsonConverter> { new IsoDateTimeConverter() }
};
var json = JsonConvert.SerializeObject(myObject, settings);