有人可以告诉我如何创建自定义转换器
Could someone tell me please how I can create a custom converter
我知道我可以使用 JSON.NET ISODateConvertor,但我想要的是具体的,我只想在响应时将值作为日/月/年"字符串发送.
I know I can use JSON.NET ISODateConvertor, but what I want is specific, I just want to send the value as "day/month/year" string on response.
像这样?
string str = JsonConvert.SerializeObject(new DateTimeClass(), new MyDateTimeConvertor());
public class DateTimeClass
{
public DateTime dt;
public int dummy = 0;
}
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("dd/MM/yyyy") );
}
}
这篇关于如何创建 JSON.NET 日期到字符串自定义转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!