javascriptserializer日期格式问题

问题描述 投票:2回答:2

我正在将一个具有许多其他类型和列表属性的复杂对象序列化为JSON形式,但问题在于DateTime属性。我使用JavascriptSerializer获得了时间(而不是mm / dd / YYYY)。

有什么方法可以获取mm / dd / YYYY:HH.MM.SS格式的日期时间,而无需修改我正在序列化的对象的类定义。

json datetime javascriptserializer
2个回答
7
投票

这不能使用JavaScriptSerializer且不修改基础类而实现。这可以通过Json.Net来实现。


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);

结果= {“日期”:“ 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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.