• <small id='9bS8u'></small><noframes id='9bS8u'>

    1. <tfoot id='9bS8u'></tfoot>
      • <bdo id='9bS8u'></bdo><ul id='9bS8u'></ul>
      <legend id='9bS8u'><style id='9bS8u'><dir id='9bS8u'><q id='9bS8u'></q></dir></style></legend>

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

      1. Json.NET 在 DateTime 上禁用反序列化

        时间:2023-08-23
          <legend id='j22Xn'><style id='j22Xn'><dir id='j22Xn'><q id='j22Xn'></q></dir></style></legend>

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

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

                <tfoot id='j22Xn'></tfoot>
                  本文介绍了Json.NET 在 DateTime 上禁用反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  代码如下:

                          string s = "2012-08-08T01:54:45.3042880+00:00";
                  
                          JObject j1 = JObject.FromObject(new
                          {
                              time=s
                          });
                  
                          Object o = j1["time"];
                  

                  我们可以检查 o 是字符串并且等于2012-08-08T01:54:45.3042880+00:00"

                  We can check that o is string and equals "2012-08-08T01:54:45.3042880+00:00"

                  现在我们将 j1.ToString() 转移到另一个程序,即

                  Now we transfer j1.ToString() to another program, which is

                         {
                            "time": "2012-08-08T01:54:45.3042880+00:00"
                         }
                  

                  然后在另一个程序中,尝试将其解析回JObject,即

                  then at the other program, try to parse it back to JObject, which is

                         JObject j2 = JObject.Parse(j1.ToString());
                  
                         Object o2 = j2["time"];
                  

                  现在,如果我们检查 o2,o2 的类型是 Date,o2.ToString() 是 8/7/2012 9:54:45 PM.

                  Now, if we check o2, o2's type is Date, o2.ToString() is 8/7/2012 9:54:45 PM.

                  我的问题是:

                  有没有办法禁用 JObject.Parse 的日期反序列化,只获取原始字符串?

                  提前致谢

                  推荐答案

                  object 解析为 JObject 时,可以指定 JsonSerializer它指示如何处理日期.

                  When parsing from an object to JObject you can specify a JsonSerializer which instructs how to handle dates.

                  JObject.FromObject(new { time = s },
                                     new JsonSerializer {
                                            DateParseHandling = DateParseHandling.None
                                     });
                  

                  不幸的是,Parse 没有这个选项,尽管拥有它是有意义的.查看 Parse 的源代码,我们可以看到它所做的只是实例化一个 JsonReader,然后将其传递给 Load.JsonReader 确实有解析选项.

                  Unfortunately Parse doesn't have this option, although it would make sense to have it. Looking at the source for Parse we can see that all it does is instantiate a JsonReader and then passes that to Load. JsonReader does have parsing options.

                  你可以像这样达到你想要的结果:

                  You can achieve your desired result like this:

                    using(JsonReader reader = new JsonTextReader(new StringReader(j1.ToString()))) {
                      reader.DateParseHandling = DateParseHandling.None;
                      JObject o = JObject.Load(reader);
                    }
                  

                  这篇关于Json.NET 在 DateTime 上禁用反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:“检测到自参考循环"JSON.Net 例外 下一篇:Json.NET 反序列化或序列化 json 字符串并将属性映

                  相关文章

                  最新文章

                    <bdo id='DJq4Q'></bdo><ul id='DJq4Q'></ul>
                  <legend id='DJq4Q'><style id='DJq4Q'><dir id='DJq4Q'><q id='DJq4Q'></q></dir></style></legend>

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

                    1. <tfoot id='DJq4Q'></tfoot>