在构造函数或者 全局开始的时候调用这个
public BossApiController() { JsonMediaTypeFormatter jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter; JsonSerializerSettings jSettings = new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Formatting.Indented, DateTimeZoneHandling = DateTimeZoneHandling.Utc }; jSettings.Converters.Add(new MyDateTimeConvertor()); jsonFormatter.SerializerSettings = jSettings; }
相关类有:
public class MyDateTimeConvertor : DateTimeConverterBase { public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return DateTime.Parse(reader.Value.ToString()); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteValue(((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss")); } }