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

      <tfoot id='YNq4O'></tfoot>
        <bdo id='YNq4O'></bdo><ul id='YNq4O'></ul>

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

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

        Newtonsoft Json.Net 序列化 JObject 不会忽略空值,即使

        时间:2023-08-24
          • <i id='16Ayt'><tr id='16Ayt'><dt id='16Ayt'><q id='16Ayt'><span id='16Ayt'><b id='16Ayt'><form id='16Ayt'><ins id='16Ayt'></ins><ul id='16Ayt'></ul><sub id='16Ayt'></sub></form><legend id='16Ayt'></legend><bdo id='16Ayt'><pre id='16Ayt'><center id='16Ayt'></center></pre></bdo></b><th id='16Ayt'></th></span></q></dt></tr></i><div id='16Ayt'><tfoot id='16Ayt'></tfoot><dl id='16Ayt'><fieldset id='16Ayt'></fieldset></dl></div>

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

                  <tfoot id='16Ayt'></tfoot>

                  <small id='16Ayt'></small><noframes id='16Ayt'>

                    <tbody id='16Ayt'></tbody>
                  本文介绍了Newtonsoft Json.Net 序列化 JObject 不会忽略空值,即使设置正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Newtonsoft Json.Net 序列化对象.

                  I'm trying to serialize an object using Newtonsoft Json.Net.

                  这个对象是一个匿名类型,里面填充了很多异构的东西,主要是常规的POCO,也有一些JObjects或者JArrays.

                  This object is an anonymous type filled with a lot of heterogenous things, mainly regular POCOs, but also some JObjects or JArrays.

                  问题是,当将 NullValueHandling 属性添加到 NullValueHandling.Ignore 时,每个 null 属性都会被忽略,但前提是它是常规".Net 对象的一部分.JObject 或 JArray 中的每个 null 属性都会保留.

                  The thing is that when adding the NullValueHandling property to NullValueHandling.Ignore, every null property gets ignored, but only if it's part of a "regular" .Net object. Every null property inside a JObject or JArray remains.

                  这是一个最小的例子:

                  var jobj = JObject.FromObject(new Anything{
                      x = 1,
                      y = "bla",
                      z = null
                  });
                  
                  var poco = new Foo {
                     foo1 = "bar",
                     foo2 = null
                  };
                  
                  var serialized = JsonConvert.SerializeObject(new {
                      source1 = poco,
                      source2 = jobj
                  }, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore});
                  

                  是否有一种简单的方法也可以忽略这些空值?我错过了一些设置选项吗?还是我必须手动处理?

                  Is there a simple way to ignore those null values as well ? Did I miss some setting option ? Or do I have to deal with it manually ?

                  推荐答案

                  JObject中的"null"值实际上是一个非空JValue 与 JValue.Type 等于 JTokenType.Null.当 JSON 中实际出现这样的值时,它表示 JSON 值 null.我相信它的存在是为了捕捉以下两个 JSON 对象之间的差异:

                  A "null" value in a JObject is actually a non-null JValue with JValue.Type equal to JTokenType.Null. It represents a JSON value of null when such a value actually appears in the JSON. I believe it exists to capture the difference between the following two JSON objects:

                    "source2": {
                      "z": null
                    }
                  
                    "source2": {
                    }
                  

                  在第一种情况下,属性 "z" 带有 null JSON 值.在第二种情况下,属性 "z" 不存在.Linq-to-JSON 表示第一个具有空类型 JValue 的情况,而不是 JProperty.Value 实际上 为空.

                  In the first case, the property "z" is present with a null JSON value. In the second case, the property "z" is not present. Linq-to-JSON represents the first case with a null-type JValue rather than having JProperty.Value actually be null.

                  为防止空标记进入您的 JObject 的值,请在从某些 POCO 创建 JObject 时使用适当的序列化程序设置:

                  To prevent null tokens from creeping into your JObject's values, use the appropriate serializer setting when creating the JObject from some POCO:

                  var jobj = JObject.FromObject(new
                  {
                      x = 1,
                      y = "bla",
                      z = (int?)null
                  }, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore } );
                  

                  (注意 POCO 本身不能是 JObject.无类型方法 JsonConvert.DeserializeObject(jsonString) 或 JsonConvert.DeserializeObject;(jsonString) 在 jsonString 中的根 JSON 容器是 JSON 对象时默认返回 JObject.)

                  (Note the POCO must not itself already be a JObject. The untyped method(s) JsonConvert.DeserializeObject(jsonString) or JsonConvert.DeserializeObject<dynamic>(jsonString) will by default return a JObject when root JSON container in jsonString is a JSON object.)

                  这篇关于Newtonsoft Json.Net 序列化 JObject 不会忽略空值,即使设置正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

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

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