如何将此属性添加到JsonConvert?

时间:2015-09-30 14:51:44

标签: c# .net json serialization json.net

这是我的代码:

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

我如何添加此属性?

new IsoDateTimeConverter()

因为它以我不喜欢的方式打印DateTime。通常我会这样做:

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

1 个答案:

答案 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);

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