• <tfoot id='dSmAN'></tfoot>

    1. <legend id='dSmAN'><style id='dSmAN'><dir id='dSmAN'><q id='dSmAN'></q></dir></style></legend>

      • <bdo id='dSmAN'></bdo><ul id='dSmAN'></ul>
      <i id='dSmAN'><tr id='dSmAN'><dt id='dSmAN'><q id='dSmAN'><span id='dSmAN'><b id='dSmAN'><form id='dSmAN'><ins id='dSmAN'></ins><ul id='dSmAN'></ul><sub id='dSmAN'></sub></form><legend id='dSmAN'></legend><bdo id='dSmAN'><pre id='dSmAN'><center id='dSmAN'></center></pre></bdo></b><th id='dSmAN'></th></span></q></dt></tr></i><div id='dSmAN'><tfoot id='dSmAN'></tfoot><dl id='dSmAN'><fieldset id='dSmAN'></fieldset></dl></div>

      <small id='dSmAN'></small><noframes id='dSmAN'>

      1. 序列化同一类中的多个 DateTime 属性,每个属性使

        时间:2023-08-22

            <bdo id='Mys2N'></bdo><ul id='Mys2N'></ul>
          • <small id='Mys2N'></small><noframes id='Mys2N'>

              <legend id='Mys2N'><style id='Mys2N'><dir id='Mys2N'><q id='Mys2N'></q></dir></style></legend>
              <i id='Mys2N'><tr id='Mys2N'><dt id='Mys2N'><q id='Mys2N'><span id='Mys2N'><b id='Mys2N'><form id='Mys2N'><ins id='Mys2N'></ins><ul id='Mys2N'></ul><sub id='Mys2N'></sub></form><legend id='Mys2N'></legend><bdo id='Mys2N'><pre id='Mys2N'><center id='Mys2N'></center></pre></bdo></b><th id='Mys2N'></th></span></q></dt></tr></i><div id='Mys2N'><tfoot id='Mys2N'></tfoot><dl id='Mys2N'><fieldset id='Mys2N'></fieldset></dl></div>

              1. <tfoot id='Mys2N'></tfoot>
                    <tbody id='Mys2N'></tbody>
                  本文介绍了序列化同一类中的多个 DateTime 属性,每个属性使用不同的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个具有两个 DateTime 属性的类.我需要用不同的格式序列化每个属性.我该怎么做?我试过了:

                  I have a class with two DateTime properties. I need to serialize each of the properties with a different format. How can I do it? I tried:

                  JsonConvert.SerializeObject(obj, Formatting.None,
                        new IsoDateTimeConverter {DateTimeFormat = "MM.dd.yyyy"});
                  

                  此解决方案对我不起作用,因为它将日期格式应用于所有属性.有没有办法用不同的格式序列化每个 DateTime 属性?也许有一些属性?

                  This solution doesn't work for me because it applies date format to all the properties. Is there any way to serialize each DateTime property with different format? Maybe there is some attribute?

                  推荐答案

                  NewtonSoft.Json 的结构有点难理解,你可以使用类似下面的自定义转换器来做你想做的事想要:

                  NewtonSoft.Json has a structure that's a bit difficult to understand, you can use something like the following custom converter to do what you want:

                  [TestMethod]
                  public void Conversion()
                  {
                      var obj = new DualDate()
                      {
                          DateOne = new DateTime(2013, 07, 25),
                          DateTwo = new DateTime(2013, 07, 25)
                      };
                      Assert.AreEqual("{"DateOne":"07.25.2013","DateTwo":"2013-07-25T00:00:00"}", 
                          JsonConvert.SerializeObject(obj, Formatting.None, new DualDateJsonConverter()));
                  }
                  
                  class DualDate
                  {
                      public DateTime DateOne { get; set; }
                      public DateTime DateTwo { get; set; }
                  }
                  
                  class DualDateJsonConverter : JsonConverter
                  {
                  
                      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                      {
                  
                          JObject result = new JObject();
                  
                          DualDate dd = (DualDate)value;
                  
                          result.Add("DateOne", JToken.FromObject(dd.DateOne.ToString("MM.dd.yyyy")));
                          result.Add("DateTwo", JToken.FromObject(dd.DateTwo));
                          result.WriteTo(writer);
                      }
                  
                      // Other JsonConverterMethods
                      public override bool CanConvert(Type objectType)
                      {
                          return objectType == typeof(DualDate);
                      }
                  
                      public override bool CanWrite
                      {
                          get
                          {
                              return true;
                          }
                      }
                      public override bool CanRead
                      {
                          get
                          {
                              return false;
                          }
                      }
                  
                      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                      {
                          throw new NotImplementedException();
                      }
                  }
                  

                  这篇关于序列化同一类中的多个 DateTime 属性,每个属性使用不同的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:事先不知道密钥的情况下,如何在 C# 中解析 JS 下一篇:反序列化DataTable后DateTime列类型变为String类型

                  相关文章

                  最新文章

                  <i id='v02nO'><tr id='v02nO'><dt id='v02nO'><q id='v02nO'><span id='v02nO'><b id='v02nO'><form id='v02nO'><ins id='v02nO'></ins><ul id='v02nO'></ul><sub id='v02nO'></sub></form><legend id='v02nO'></legend><bdo id='v02nO'><pre id='v02nO'><center id='v02nO'></center></pre></bdo></b><th id='v02nO'></th></span></q></dt></tr></i><div id='v02nO'><tfoot id='v02nO'></tfoot><dl id='v02nO'><fieldset id='v02nO'></fieldset></dl></div>
                  <tfoot id='v02nO'></tfoot>
                    <legend id='v02nO'><style id='v02nO'><dir id='v02nO'><q id='v02nO'></q></dir></style></legend>

                    <small id='v02nO'></small><noframes id='v02nO'>

                    • <bdo id='v02nO'></bdo><ul id='v02nO'></ul>