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

火星漫游者II

如何在序列化中忽略属性,但在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属性值的值将“内容”反序列化为属性“ RelationshipContent”和“ DomainContent”。

您能帮我使用Newtonsoft.Json在C#中做到这一点吗?

g

您能否提供一种“设置”Content并将数据处理到正确位置的方法。

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; }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章