<tfoot id='Xh0lt'></tfoot>

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

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

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

        JSON.net 中缺少的属性的默认值

        时间:2023-08-22

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

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

            <tbody id='Wcehy'></tbody>

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

                • 本文介绍了JSON.net 中缺少的属性的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 Json.net 将对象序列化到数据库.

                  I'm using Json.net to serialize objects to database.

                  我向类中添加了一个新属性(数据库中的 json 中缺少该属性),并且我希望新属性在 json 中缺少时具有默认值.

                  I added a new property to the class (which is missing in the json in the database) and I want the new property to have a default value when missing in the json.

                  我尝试了 DefaultValue 属性,但它不起作用.我正在使用私有设置器和构造器来反序列化json,因此在构造器中设置属性的值将不起作用,因为有一个带有值的参数.

                  I tried DefaultValue attribute but it's doesn't work. I'm using private setters and constructor to deserialize the json, so setting the value of the property in the constructor will not work as there is a parameter with the value.

                  以下是一个例子:

                      class Cat
                      {
                          public Cat(string name, int age)
                          {
                              Name = name;
                              Age = age;
                          }
                  
                          public string Name { get; private set; }
                  
                          [DefaultValue(5)]            
                          public int Age { get; private set; }
                      }
                  
                      static void Main(string[] args)
                      {
                          string json = "{"name":"mmmm"}";
                  
                          Cat cat = JsonConvert.DeserializeObject<Cat>(json);
                  
                          Console.WriteLine("{0} {1}", cat.Name, cat.Age);
                      }
                  

                  我预计年龄是 5 岁,但它是零.

                  I expect the age to be 5 but it is zero.

                  有什么建议吗?

                  推荐答案

                  我找到了答案,只需要添加以下属性:

                  I found the answer, just need to add the following attribute as well:

                  [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
                  

                  在你的例子中:

                  class Cat
                  {
                      public Cat(string name, int age)
                      {
                          Name = name;
                          Age = age;
                      }
                  
                      public string Name { get; private set; }
                  
                      [DefaultValue(5)]            
                      [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
                      public int Age { get; private set; }
                  }
                  
                  static void Main(string[] args)
                  {
                      string json = "{"name":"mmmm"}";
                  
                      Cat cat = JsonConvert.DeserializeObject<Cat>(json);
                  
                      Console.WriteLine("{0} {1}", cat.Name, cat.Age);
                  }
                  

                  请参阅 Json.Net 参考

                  这篇关于JSON.net 中缺少的属性的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:车牌检测有哪些好的算法? 下一篇:如何更改数字反序列化的默认类型?

                  相关文章

                  最新文章

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

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

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

                      <tfoot id='oR1m0'></tfoot>