检测到 JSON.Net 自引用循环 [英] JSON.Net Self referencing loop detected
问题描述
我的网站有一个 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.CyberUser"的属性CyberUser"的自引用循环.路径[0].EventRegistrations[0].CyberUser.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 codeplex 页面:
it also referes to the Json.NET codeplex page at:
http://json.codeplex.com/discussions/272371
Documentation: ReferenceLoopHandling setting
这篇关于检测到 JSON.Net 自引用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!