首页 > 解决方案 > 属性未通过 newtonsoft 在 Json 中序列化

问题描述

类中定义了许多属性,但其中一个属性没有被序列化(使用 newtonsoft),下面提到了该属性:

public override string Value
{​​​​
  set    
  {
​​​​
    if (value == null)    
    {    ​​​​
       this.Null = true;    
    }​​ ​ ​
    else if (value == string.Empty)
    {    ​​​​
       _A = string.Empty;    
       _P = string.Empty;    
       _S = string.Empty;    
    }​​​ ​
    else
    {​​​ ​

         value = _A + _P + _S;    
     }​​​​

   }​​​​    
}

但是当我们在属性中定义 getter 时,它会被序列化,所以请建议;我们不想将 getter 放在属性中。

标签: c#.netasp.net-core.net-corevisual-studio-2019

解决方案


[JsonProperty]属性设置为您的属性。但总的来说,你可以让你的吸气剂private- 所以你不会将数据暴露给其他类,但它可以帮助解决你的问题。


推荐阅读