6

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

有什么方法可以在不修改我正在序列化的对象的类定义的情况下以 mm/dd/YYYY : HH.MM.SS 形式获取日期时间。

4

2 回答 2

10

这不能使用JavaScriptSerializer和不修改底层类来实现。这可以通过Json.Net来实现。

于 2010-10-28T17:56:32.817 回答
2

使用序列化器对象上的 RegisterConverters 方法可以解决此问题。

.NET JavaScriptSerializer 的自定义 DateTime JSON 格式

您只需创建一个继承 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"));

为您的日期时间的自定义版本。

链接中的课程:

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();
        }
    }
}
于 2019-10-25T11:50:36.510 回答