在 .NET JSON 序列化期间忽略字段;类似于 [XmlIgnore]? [英] Ignoring a field during .NET JSON serialization; similar to [XmlIgnore]?
问题描述
我有一个 POCO 类,它在 .NET 3.5 sp1 中作为 JSON 字符串发送到浏览器.我只是使用默认的 JSON 序列化,我有一些我想忽略的字段.我想在它们上面放一个类似于 [System.Xml.Serialization.XmlIgnore] 的属性,这样它们就不会被序列化.
I have a POCO class that is being sent to the browser as a JSON string in .NET 3.5 sp1. I am just using the default JSON serialization and I have some fields that I want to ignore. I want to put an attribute similar to [System.Xml.Serialization.XmlIgnore] on them so that they are not serialized.
推荐答案
我使用 ScriptIgnore 属性像这样:
I use the ScriptIgnore attribute on my model like so:
public class Item
{
[ScriptIgnore]
public Item ParentItem { get; set; }
}
在这个特定的场景中,我从 Json 序列化器中得到一个循环引用错误,所以我只是忽略了它.我在在这里问了一个类似的问题 当我发现 Model 和 ViewModel 之间的区别时.
In this particular scenario I was getting a circular reference error from the Json serializer, so I simply ignored it. I was asking a similar question here on SO when I was turned on to the difference between a Model and ViewModel.
这篇关于在 .NET JSON 序列化期间忽略字段;类似于 [XmlIgnore]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!