检测到 JSON.Net 自引用循环 [英] JSON.Net Self referencing loop detected

查看:39
本文介绍了检测到 JSON.Net 自引用循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站有一个 mssql 数据库,包含 4 个表.

I have a mssql database for my website within 4 tables.

当我使用它时:

public static string GetAllEventsForJSON()
{
    using (CyberDBDataContext db = new CyberDBDataContext())
    {
        return JsonConvert.SerializeObject((from a in db.Events where a.Active select a).ToList(), new JavaScriptDateTimeConverter());
    }
}

代码导致以下错误:

Newtonsoft.Json.JsonSerializationException:检测到类型为DAL.Cyber​​User"的属性Cyber​​User"的自引用循环.路径[0].EventRegistrations[0].Cyber​​User.UserLogs[0]".

Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'CyberUser' with type 'DAL.CyberUser'. Path '[0].EventRegistrations[0].CyberUser.UserLogs[0]'.

推荐答案

我刚刚遇到了与父/子集合相同的问题,并找到了解决我的问题的帖子.我只想显示父集合项的列表,不需要任何子数据,因此我使用了以下内容并且效果很好:

I just had the same problem with Parent/Child collections and found that post which has solved my case. I Only wanted to show the List of parent collection items and didn't need any of the child data, therefore i used the following and it worked fine:

JsonConvert.SerializeObject(ResultGroups, Formatting.None,
                        new JsonSerializerSettings()
                        { 
                            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                        });

检测到类型的 JSON.NET 错误自引用循环

它还指的是 Json.NET codeplex 页面:

it also referes to the Json.NET codeplex page at:

http://json.codeplex.com/discussions/272371

文档:ReferenceLoopHandling 设置

Documentation: ReferenceLoopHandling setting

这篇关于检测到 JSON.Net 自引用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆