首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >javascriptserializer日期格式问题

javascriptserializer日期格式问题
EN

Stack Overflow用户
提问于 2010-10-29 01:49:44
回答 2查看 9.2K关注 0票数 6

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

有没有办法在不修改序列化对象的类定义的情况下获得mm/dd/YYYY : HH.MM.SS格式的datetime?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-10-29 01:56:33

在不修改底层类的情况下,使用JavaScriptSerializer无法实现这一点。这可以通过Json.Net来实现。

票数 10
EN

Stack Overflow用户

发布于 2019-10-25 19:50:37

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

Custom DateTime JSON Format for .NET JavaScriptSerializer

您只需创建一个继承JavaScriptConverter的类,并实现您自己的DateTime对象序列化。

然后像这样序列化:

代码语言:javascript
复制
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"}

更改线路

代码语言:javascript
复制
return new CustomString(((DateTime)obj).ToUniversalTime().ToString("O"));

用于您的自定义版本的DateTime。

链接中的类:

代码语言:javascript
复制
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();
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4045710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档