2015-09-30 95 views
0

這是我的代碼有:如何將此屬性添加到JsonConvert?

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

如何我還添加該屬性?

new IsoDateTimeConverter() 

因爲它以我不喜歡的方式打印DateTime。通常我做:

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

回答

1

轉換器添加到Converters集合JsonSerializerSettings對象。

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

+0

JsonSerializerSettings不包含格式的定義。 – markzzz

+0

不正確。請參閱[文檔](http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonSerializerSettings.htm)。 –