循环引用和ScriptIgnore问题

发布于 2025-01-04 17:48:32 字数 917 浏览 3 评论 0原文

我有几个相互引用的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○愚か者の日 2025-01-11 17:48:32

我为防止该错误所做的就是返回一个反映对象属性的匿名类型,如下所示:

    public JsonResult CompanyListJson()
    {
        var list = (from companies in appContext.Companies
                    where companies.Active.Equals(true)
                    select new
                    {
                        Name = companies.DbName,
                        Id = companies.Id,
                        Active = companies.Id.Equals(CurrentCompany.Id)

                    });
        return Json(list, JsonRequestBehavior.AllowGet);
    }

也许这不是最好的方法,但它允许我保持 JSON 的精简并仅推送我需要的数据(并避免这种情况)当然,循环引用例外)

查看您的示例,我会从 SearchAnalysis 中选择新的匿名类型,获取我需要的属性。那应该有效

What I'm doing to prevent that error is to return an anonymouse type that reflects my object's properties like this :

    public JsonResult CompanyListJson()
    {
        var list = (from companies in appContext.Companies
                    where companies.Active.Equals(true)
                    select new
                    {
                        Name = companies.DbName,
                        Id = companies.Id,
                        Active = companies.Id.Equals(CurrentCompany.Id)

                    });
        return Json(list, JsonRequestBehavior.AllowGet);
    }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文