【问题标题】:javascriptserializer date format issuejavascriptserializer 日期格式问题
【发布时间】:2010-10-28 17:49:44
【问题描述】:

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

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

【问题讨论】:

    标签: json datetime javascriptserializer


    【解决方案1】:

    使用JavaScriptSerializer 并且不修改底层类是无法实现的。这可以通过Json.Net 来实现。

    【讨论】:

      【解决方案2】:

      使用序列化器对象上的 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();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-20
        • 2014-04-01
        • 1970-01-01
        • 2016-01-25
        相关资源
        最近更新 更多