开发者

Omit object property during json serialization

开发者 https://www.devze.com 2023-04-07 17:04 出处:网络
I\'m using MVC Controller. Json method in order to send json result object to my javascript function.

I'm using MVC Controller. Json method in order to send json result object to my javascript function.

From time to time I want to omit one of my object's property from the json result object.

How can I achive it?

This is my .NET object:

    public class jsTreeModel
    {
        public string Data { get; set; }
        public JsTree开发者_运维问答Attribute Att { get; set; }
        public string State { get; set; }
        public List<jsTreeModel> Children { get; set; }
    }

I this case I want to omit the 'Children' property from the json result.

Any idea?


If you are using Json.NET as your serializer, you can omit a property fairly simply:

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

I hope this helps you!

0

精彩评论

暂无评论...
验证码 换一张
取 消