本文介绍了忽略在.NET JSON序列化的字段;类似于[XmlIgnore]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被发送到浏览器作为一个JSON字符串在.NET 3.5 SP1的POCO类。我只是使用默认的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序列得到一个循环引用错误,所以我只是忽略了它。我是<一个href="http://stackoverflow.com/questions/1269106/how-do-i-$p$pvent-json-serialization-in-asp-net-mvc">asking在这里SO 当我开到模型和视图模型之间的区别了类似的问题。

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]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 20:35