【问题标题】:Omit object property during json serialization在 json 序列化期间省略对象属性
【发布时间】:2011-11-23 18:59:37
【问题描述】:

我正在使用 MVC 控制器。 Json 方法,以便将 json 结果对象发送到我的 javascript 函数。

有时我想从 json 结果对象中省略我的对象属性之一。

我怎样才能实现它?

这是我的 .NET 对象:

    public class jsTreeModel
    {
        public string Data { get; set; }
        public JsTreeAttribute Att { get; set; }
        public string State { get; set; }
        public List<jsTreeModel> Children { get; set; }
    }

在这种情况下,我想从 json 结果中省略 'Children' 属性。

有什么想法吗?

【问题讨论】:

标签: c# javascript json model-view-controller


【解决方案1】:

如果您使用 Json.NET 作为序列化程序,则可以相当简单地省略一个属性:

public class jsTreeModel
{
    public string Data { get; set; }
    public JsTreeAttribute Att { get; set; }
    public string State { get; set; }
    [JsonIgnore]
    public List<jsTreeModel> Children { get; set; }
}

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2013-04-11
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多