<i id='E4255'><tr id='E4255'><dt id='E4255'><q id='E4255'><span id='E4255'><b id='E4255'><form id='E4255'><ins id='E4255'></ins><ul id='E4255'></ul><sub id='E4255'></sub></form><legend id='E4255'></legend><bdo id='E4255'><pre id='E4255'><center id='E4255'></center></pre></bdo></b><th id='E4255'></th></span></q></dt></tr></i><div id='E4255'><tfoot id='E4255'></tfoot><dl id='E4255'><fieldset id='E4255'></fieldset></dl></div>
    1. <legend id='E4255'><style id='E4255'><dir id='E4255'><q id='E4255'></q></dir></style></legend><tfoot id='E4255'></tfoot>
    2. <small id='E4255'></small><noframes id='E4255'>

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

        如何将时区偏移量添加到 JSON.NET 序列化?

        How to add timezone offset to JSON.NET serialization?(如何将时区偏移量添加到 JSON.NET 序列化?)

            <legend id='mQt7N'><style id='mQt7N'><dir id='mQt7N'><q id='mQt7N'></q></dir></style></legend>

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

                • <bdo id='mQt7N'></bdo><ul id='mQt7N'></ul>
                    <tbody id='mQt7N'></tbody>
                • <small id='mQt7N'></small><noframes id='mQt7N'>

                  本文介绍了如何将时区偏移量添加到 JSON.NET 序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我的 DateTimePicker 绑定到属性:

                  My DateTimePicker is bound to property:

                      picker.DataBindings.Add("Value", this, "EventDate");
                      ...
                      private DateTime eventDate;
                      public DateTime EventDate
                      {
                          get
                          {
                              var offset = TimeZoneInfo.Local.GetUtcOffset(eventDate);
                              string json = JsonConvert.SerializeObject(eventDate, Formatting.Indented);
                              Console.WriteLine("get: {0} --- {1}", json, offset);
                              return eventDate;
                          }
                          set
                          {
                              var offset = TimeZoneInfo.Local.GetUtcOffset(value);
                              string json = JsonConvert.SerializeObject(value, Formatting.Indented);
                              Console.WriteLine("set: {0} --- {1}", json, offset);
                              eventDate = value;
                          }
                      }
                  

                  每当数据绑定设置属性时,我得到以下结果:

                  Whenever data binding is to set the property, I've got the following result:

                  set: "2015-08-03T16:06:59" --- 04:00:00
                  get: "2015-08-03T16:06:59" --- 04:00:00
                  

                  get/set 中的代码仅用于调试目的.整个类与 EventDate 属性一起序列化.

                  The code in get/set only for debug purposes. The whole class is serialized along with EventDate property.

                  如何修改 DateTime 变量和/或 json 序列化程序以包含时区偏移,例如:

                  How can I modify DateTime variable and/or json serializer in order to include timezone offset such as:

                  "2014-08-03T16:06:59.8708232+04:00"
                  

                  奇怪的是,如果我创建全新的 DateTime 对象并分配给 DateTime.Now 而不绑定,JSON.NET 将为其附加时区偏移量.不知道有什么区别.

                  Strange thing is if I create brand new DateTime object and assign to DateTime.Now without binding, JSON.NET will append timezone offset to it. Can't figure out what is the difference.

                  推荐答案

                  你试过类型 DateTimeOffset 而不是 DateTime?这是带有时区信息的类型.

                  Have you tried type DateTimeOffset instead of DateTime? This is the type with timezone information.

                  var example = new
                  {
                      Now = DateTimeOffset.Now,
                      UtcNow = DateTimeOffset.UtcNow,
                      Sometime = new DateTimeOffset(2014, 10, 11, 1, 4, 9, new TimeSpan(8, 0, 0)),
                      FromDateTime = new DateTimeOffset(new DateTime(2010, 1, 1)),
                  };
                  string json = JsonConvert.SerializeObject(example, Formatting.Indented);
                  Console.WriteLine(json);
                  

                  给我:

                  {
                    "Now": "2014-08-03T22:08:47.8127753+08:00",
                    "UtcNow": "2014-08-03T14:08:47.8137754+00:00",
                    "Sometime": "2014-10-11T01:04:09+08:00",
                    "FromDateTime": "2010-01-01T00:00:00+08:00"
                  }
                  

                  编辑 - 替代方式

                  您注意到 DateTime.Now 在 JSON 中有时区偏移,而手动创建的 DateTime 没有.这是因为 DateTime.NowKind 等于 DateTimeKind.Local 而另一个具有 DateTimeKind.Unspecified,并且JSON.NET 只是以不同的方式处理它们.

                  You noticed that DateTime.Now has timezone offset in JSON while manually created DateTime does not. This is because DateTime.Now has Kind equal to DateTimeKind.Local while the other has DateTimeKind.Unspecified, and JSON.NET just handles them differently.

                  所以另一种方法是:

                  var product2 = new
                  {
                      Now = DateTime.Now,
                      Sometime = new DateTime(2014, 10, 11, 0, 0, 0),
                      SometimeLocal = new DateTime(2014, 10, 11, 0, 0, 0, DateTimeKind.Local),
                      SometimeUtc = new DateTime(2014, 10, 11, 0, 0, 0, DateTimeKind.Utc)
                  };
                  
                  string json2 = JsonConvert.SerializeObject(product2, Formatting.Indented);
                  
                  Console.WriteLine(json2);
                  

                  哪些输出:

                  {
                    "Now": "2014-08-03T23:39:45.3319275+08:00",
                    "Sometime": "2014-10-11T00:00:00",
                    "SometimeLocal": "2014-10-11T00:00:00+08:00",
                    "SometimeUtc": "2014-10-11T00:00:00Z"
                  }
                  

                  EDIT2 - 用于数据绑定

                  好的,你使用数据绑定.然后您可以更新您的设置器以自动转换:

                  OK you use data binding. Then you can update your setter to convert automatically:

                  picker.DataBindings.Add("Value", this, "EventDate");
                  ...
                  private DateTimeOffset eventDate;
                  public DateTime EventDate
                  {
                      get { return eventDate.LocalDateTime; }
                      set { eventDate = new DateTimeOffset(value); }
                  }
                  

                  然后就可以使用eventDate序列化成JSON了.

                  Then you can use eventDate to serialize to JSON.

                  或设置种类:

                  picker.DataBindings.Add("Value", this, "EventDate");
                  ...
                  private DateTime eventDate;
                  public DateTime EventDate
                  {
                      get { return eventDate; }
                      set
                      {
                          // Create a copy of DateTime and set Kind to Local since Kind is readonly
                          eventDate = new DateTime(value.Ticks, DateTimeKind.Local);
                      }
                  }
                  

                  两者都应该工作.

                  这篇关于如何将时区偏移量添加到 JSON.NET 序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)
                  • <i id='sAxAQ'><tr id='sAxAQ'><dt id='sAxAQ'><q id='sAxAQ'><span id='sAxAQ'><b id='sAxAQ'><form id='sAxAQ'><ins id='sAxAQ'></ins><ul id='sAxAQ'></ul><sub id='sAxAQ'></sub></form><legend id='sAxAQ'></legend><bdo id='sAxAQ'><pre id='sAxAQ'><center id='sAxAQ'></center></pre></bdo></b><th id='sAxAQ'></th></span></q></dt></tr></i><div id='sAxAQ'><tfoot id='sAxAQ'></tfoot><dl id='sAxAQ'><fieldset id='sAxAQ'></fieldset></dl></div>
                      <tfoot id='sAxAQ'></tfoot><legend id='sAxAQ'><style id='sAxAQ'><dir id='sAxAQ'><q id='sAxAQ'></q></dir></style></legend>

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

                        <bdo id='sAxAQ'></bdo><ul id='sAxAQ'></ul>
                            <tbody id='sAxAQ'></tbody>