循环引用和ScriptIgnore问题
我有几个相互引用的 BusinessObject 类,我需要在 JsonResponse 中序列化一个类并将其返回到我的视图。我不断收到循环引用异常,但无法摆脱它。我已将 [ScriptIgnore()]
装饰器放置在每个不是简单数据类型属性的属性上,但我仍然收到异常。我不知道问题出在哪里,因为我阻止了序列化器的几乎所有操作,而且它仍然在我身上爆炸。
有什么方法可以查看序列化对象的当前状态吗?
[HttpPost]
public JsonResult GetAnalysisInfo(int id)
{
ProjectContext myDB = db;
SearchAnalysis searchanalysis = SearchAnalysis.Find(myDB, id);
//searchanalysis.Results = searchanalysis.SearchResultsPrototype(myDB);
return this.Json(searchanalysis);
}
更新
我也尝试实施 ISerialized 但无济于事。我的 GetObjectData 非常简单:
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("SearchAnalysisId", this.SearchAnalysisId);
info.AddValue("Created", this.Created);
}
仍然收到 CircularReference 错误。 DateTime 数据类型不会导致序列化出现问题,对吧?
I have several BusinessObject classes that refer to each other and I need to serialize one in a JsonResponse and return it to my view. I keep getting a circular reference exception and I cannot get rid of it. I have placed the [ScriptIgnore()]
decorator on every property that is not a simple data type property and I am still getting the exception. I cant figure out where the problem is, as I am blocking the serializer from just about everything and it is still blowing up on me.
Is there any way to see what they current state of the serialized object is?
[HttpPost]
public JsonResult GetAnalysisInfo(int id)
{
ProjectContext myDB = db;
SearchAnalysis searchanalysis = SearchAnalysis.Find(myDB, id);
//searchanalysis.Results = searchanalysis.SearchResultsPrototype(myDB);
return this.Json(searchanalysis);
}
Update
I have also tried implementing ISerializable to no avail. My GetObjectData is very simple:
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("SearchAnalysisId", this.SearchAnalysisId);
info.AddValue("Created", this.Created);
}
Still getting a CircularRefernce error. DateTime data types don't cause problems with Serialization right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我为防止该错误所做的就是返回一个反映对象属性的匿名类型,如下所示:
也许这不是最好的方法,但它允许我保持 JSON 的精简并仅推送我需要的数据(并避免这种情况)当然,循环引用例外)
查看您的示例,我会从 SearchAnalysis 中选择新的匿名类型,获取我需要的属性。那应该有效
What I'm doing to prevent that error is to return an anonymouse type that reflects my object's properties like this :
Maybe it's not the best way, but it allows me to keep my JSON thin and push only the data I need ( and avoid that circular reference exception of course )
Looking at your sample, I would select new anonymouse type from SearchAnalysis, taking the properties I need. That should work