如何从序列化中忽略 属性 但在 c# 中使用 json.net 保留反序列化

How to ignore a property from serialization but keep in for deserialization using json.net in c#

如何在 c# 中使用 json.net 从序列化中忽略 属性 但保留反序列化。

示例:

public class Foo
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("contentType")]
    public object ContentType { get; set; }

    [JsonProperty("content")]
    public object Content { get; set; }

    [JsonIgnore]
    public Relationship RelationshipContent { get; set; }

    [JsonIgnore]
    public Domains DomainContent { get; set; }

}

在上面的代码中,我需要根据 PartType 属性 值的值将 "content" 反序列化为 属性 "RelationshipContent" 和 "DomainContent" .

你能帮助我如何使用 Newtonsoft.Json 在 C# 中做到这一点吗?

你能为 Content 的 'set' 提供一种方法并将数据处理到正确的位置吗?

public class Foo
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("contentType")]
    public object ContentType { get; set; }

    [JsonProperty("content")]
    public object Content { get; set {
            //do something here with `value` e.g.
            RelationshipContent = value; //change as required
            DomainContent = value; //change as required
        }
    }

    [JsonIgnore]
    public Relationship RelationshipContent { get; set; }

    [JsonIgnore]
    public Domains DomainContent { get; set; }

}