我正在将具有其他类型和列表的许多属性的复杂对象序列化为JSON格式,但问题在于DateTime属性。我用JavascriptSerializer获得了纪元时间(而不是mm / dd / YYYY)。
有没有什么方法可以获得mm / dd / YYYY:HH.MM.SS形式的日期时间而不修改我正在序列化的对象的类别定义。
答案 0 :(得分:6)
使用JavaScriptSerializer
并且不修改基础类无法实现。这可以通过Json.Net实现。
答案 1 :(得分:1)
这实际上可以通过这个黑客来实现:
http://blog.calyptus.eu/seb/2011/12/custom-datetime-json-serialization/
它将DateTime转换为序列化为字符串的Uri类。
答案 2 :(得分:0)
使用序列化程序对象上的RegisterConverters方法可以解决此问题。
Custom DateTime JSON Format for .NET JavaScriptSerializer
您只需创建一个继承JavaScriptConverter的类并实现您自己的DateTime对象的序列化。
然后像这样序列化:
var obj = new { date = DateTime.Now };
var ser = new JavaScriptSerializer();
ser.RegisterConverters(new[] { new DateTimeJavaScriptConverter() });
var result = ser.Serialize(obj);
result = {“ date”:“ 2019-10-25T11:49:58.7322411Z”}
更改行
return new CustomString(((DateTime)obj).ToUniversalTime().ToString("O"));
您的自定义版本的DateTime。
链接中的类:
public class DateTimeJavaScriptConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
return new JavaScriptSerializer().ConvertToType(dictionary, type);
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
if (!(obj is DateTime)) return null;
return new CustomString(((DateTime)obj).ToUniversalTime().ToString("O"));
}
public override IEnumerable<Type> SupportedTypes
{
get { return new[] { typeof(DateTime) }; }
}
private class CustomString : Uri, IDictionary<string, object>
{
public CustomString(string str)
: base(str, UriKind.Relative)
{
}
void IDictionary<string, object>.Add(string key, object value)
{
throw new NotImplementedException();
}
bool IDictionary<string, object>.ContainsKey(string key)
{
throw new NotImplementedException();
}
ICollection<string> IDictionary<string, object>.Keys
{
get { throw new NotImplementedException(); }
}
bool IDictionary<string, object>.Remove(string key)
{
throw new NotImplementedException();
}
bool IDictionary<string, object>.TryGetValue(string key, out object value)
{
throw new NotImplementedException();
}
ICollection<object> IDictionary<string, object>.Values
{
get { throw new NotImplementedException(); }
}
object IDictionary<string, object>.this[string key]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
{
throw new NotImplementedException();
}
void ICollection<KeyValuePair<string, object>>.Clear()
{
throw new NotImplementedException();
}
bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
{
throw new NotImplementedException();
}
void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
int ICollection<KeyValuePair<string, object>>.Count
{
get { throw new NotImplementedException(); }
}
bool ICollection<KeyValuePair<string, object>>.IsReadOnly
{
get { throw new NotImplementedException(); }
}
bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
{
throw new NotImplementedException();
}
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
}