簡體   English   中英

屬性未通過 newtonsoft 在 Json 中序列化

[英]Property not getting serialized in Json via newtonsoft

類中定義了許多屬性,但其中一個屬性沒有被序列化(使用 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 時,它會被序列化,所以請提出建議; 我們不想將吸氣劑放在屬性中。

[JsonProperty]屬性設置為您的屬性。 但總的來說,您可以將您的 getter 設為private - 這樣您就不會將數據暴露給其他類,但它可以幫助解決您的問題。

好吧,首先,您沒有 Get how will json now to serialize it,也許使它成為一個方法看起來很有趣,因為它只有一個屬性集。 如果你想填充你的字段,只需告訴 json 使用注釋來做到這一點

[JsonProperty("_A")]
private string _A ;

[JsonProperty("_P")]
private string _P ;

[JsonProperty("_S")]
private string _S;

[JsonIgnore]
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;    
     }​​​​

   }​​​​    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM