首页 > 代码库 > Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案
Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案
使用Newtonsoft.Json 转换DateTime类型时,若使用标准转换,则字符串内会有一个T(虽然再转换成DateTime没有问题)。
若要转换成DateTime没有T,可以加上特性:
1 public class TestClass 2 { 3 public TestClass() 4 { 5 Dt1 = Dt2 = new DateTime(1900, 1, 1, 0, 0, 0); 6 } 7 8 [JsonConverter(typeof(TcDateTimeConverter))] 9 public DateTime Dt1 { get; set; } 10 11 public DateTime Dt2 { get; set; } 12 } 13 14 15 16 /// <summary> 17 /// DateTime Json序列化格式:yyyy-MM-dd HH:mm:ss 18 /// </summary> 19 public class TcFullDateTimeConverter : DateTimeConverterBase 20 { 21 private static IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }; 22 23 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 24 { 25 return dtConverter.ReadJson(reader, objectType, ConvertHelper.ToDateTime(existingValue), serializer); 26 } 27 28 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 29 { 30 dtConverter.WriteJson(writer, ConvertHelper.ToDateTime(value), serializer); 31 } 32 } 33 34 /// <summary> 35 /// DateTime Json序列化格式:yyyy-MM-dd HH:mm 36 /// </summary> 37 public class TcDateTimeConverter : DateTimeConverterBase 38 { 39 private static IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm" }; 40 41 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 42 { 43 return dtConverter.ReadJson(reader, objectType, ConvertHelper.ToDateTime(existingValue), serializer); 44 } 45 46 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 47 { 48 dtConverter.WriteJson(writer, ConvertHelper.ToDateTime(value), serializer); 49 } 50 } 51 52 /// <summary> 53 /// DateTime Json序列化格式:yyyy-MM-dd 54 /// </summary> 55 public class TcDateConverter : DateTimeConverterBase 56 { 57 private static IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" }; 58 59 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 60 { 61 return dtConverter.ReadJson(reader, objectType, ConvertHelper.ToDateTime(existingValue), serializer); 62 } 63 64 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 65 { 66 dtConverter.WriteJson(writer, ConvertHelper.ToDateTime(value), serializer); 67 } 68 } 69 70 /// <summary> 71 /// DateTime Json序列化格式:HH:mm 72 /// </summary> 73 public class TcTimeConverter : DateTimeConverterBase 74 { 75 private static IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "HH:mm" }; 76 77 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 78 { 79 return dtConverter.ReadJson(reader, objectType, ConvertHelper.ToDateTime(existingValue), serializer); 80 } 81 82 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 83 { 84 dtConverter.WriteJson(writer, ConvertHelper.ToDateTime(value), serializer); 85 } 86 }
另外,若某字段如上加上TcDateTimeConverter,再转换为带TcDateTimeConverter特性或者不带该特性的标准DateTime,都没有问题。
Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。