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

      1. <small id='s1cJn'></small><noframes id='s1cJn'>

        <legend id='s1cJn'><style id='s1cJn'><dir id='s1cJn'><q id='s1cJn'></q></dir></style></legend>
      2. <tfoot id='s1cJn'></tfoot>

      3. Json.net 无法加载属于类对象的某些属性?

        时间:2023-05-22

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

                1. 本文介绍了Json.net 无法加载属于类对象的某些属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  注意:我添加此社区 wiki 条目是为了节省我刚刚调试此问题的时间.

                  NOTE: I'm adding this community wiki entry to save someone the loss of time I just went through debugging this problem.

                  我有一个具有多个公共属性的类对象.我可以使用 JSON.net 很好地序列化它.但是当我加载 JSON 文本并使用 JsonConvert.DeserializeObject<> 对其进行反序列化时,某些字段在当时确实具有有效值时被设置为 NULL的序列化.我手动检查了序列化的 JSON 字符串,我肯定在文本中看到了 NULL 属性的值.为什么会这样?

                  I have a class object with multiple public properties. I can serialize it fine using JSON.net. But when I load the JSON text back and deserialize it using JsonConvert.DeserializeObject<>, some of the fields are set to NULL when they definitely had valid values at the time of serialization. I inspected the serialized JSON string manually and I definitely see values for the NULL properties in the text. Why is this happening?

                  推荐答案

                  默认情况下,Json.Net 只对类的公共成员进行序列化和反序列化.如果您的属性有公共 getter 和私有 setter,那么这些属性将被序列化为 JSON,但不会反序列化回您的类.

                  By default, Json.Net serializes and deserializes only the public members of a class. If you have public getters but private setters for your properties then the properties will be serialized to JSON but not deserialized back to your class.

                  解决此问题的简单方法是将您的 setter 公开,但这当然会破坏私有 setter 提供的不变性.如果您希望在仍然能够反序列化它们的同时保持您的 setter 私有,您可以使用 [JsonProperty] 属性来注释您的属性.这将允许反序列化器看到"它们.

                  The easy way to fix this is to make your setters public, but of course that breaks the immutability that private setters provide. If you want to be able to keep your setters private while still being able to deserialize them, you can annotate your properties with [JsonProperty] attributes instead. This will allow the deserializer to "see" them.

                  这里有一个简短的小提琴来演示:https://dotnetfiddle.net/4nZdGJ

                  Here is a short fiddle to demonstrate: https://dotnetfiddle.net/4nZdGJ

                  这篇关于Json.net 无法加载属于类对象的某些属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:对属性的 Json DeserializeObject 运行转换 下一篇:如何添加元数据来描述 JSON.Net 中哪些属性是日期

                  相关文章

                  • <bdo id='55Pzz'></bdo><ul id='55Pzz'></ul>
                  <legend id='55Pzz'><style id='55Pzz'><dir id='55Pzz'><q id='55Pzz'></q></dir></style></legend>

                    <small id='55Pzz'></small><noframes id='55Pzz'>

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

                    1. <tfoot id='55Pzz'></tfoot>