I have a mssql database for my website within 4 tables.
我的网站有一个mssql数据库,包含4个表。
When I use this:
当我用这个:
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());
}
}
The code results in the following error:
该代码导致以下错误:
Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'CyberUser' with type 'DAL.CyberUser'. Path '[0].EventRegistrations[0].CyberUser.UserLogs[0]'.
Newtonsoft.Json。JsonSerializationException:为属性“CyberUser”检测的自引用循环,类型为“DAL.CyberUser”。路径”[0].EventRegistrations[0].CyberUser.UserLogs[0]”。
5 个解决方案
#1
130
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 Error Self referencing loop detected for type
JSON。为类型检测到的净错误自引用循环
it also referes to the Json.NET codeplex page at:
它还引用Json。净codeplex上页面:
http://json.codeplex.com/discussions/272371
http://json.codeplex.com/discussions/272371
Documentation: ReferenceLoopHandling setting
文档:ReferenceLoopHandling设置
#2
33
The fix is to ignore loop references and not to serialize them. This behaviour is specified in JsonSerializerSettings
.
修复方法是忽略循环引用,而不是序列化它们。此行为在JsonSerializerSettings中指定。
Single JsonConvert
with an overload:
带有重载的单个JsonConvert:
JsonConvert.SerializeObject((from a in db.Events where a.Active select a).ToList(), Formatting.Indented,
new JsonSerializerSettings() {
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
}
);
If you'd like to make this the default behaviour, add a Global Setting with code in Application_Start()
in Global.asax.cs:
如果您想让这成为默认行为,请在Global.asax.cs .cs .中添加一个带有代码的Application_Start()全局设置:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings {
Formatting = Newtonsoft.Json.Formatting.Indented,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
};
Reference: https://github.com/JamesNK/Newtonsoft.Json/issues/78
参考:https://github.com/JamesNK/Newtonsoft.Json/issues/78
#3
14
If using ASP.NET Core MVC, add this to the ConfigureServices method of your startup.cs file:
如果使用ASP。NET Core MVC,将它添加到启动的ConfigureServices方法中。cs文件:
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
#4
12
This may help you.
这可能会帮助你。
public MyContext() : base("name=MyContext")
{
Database.SetInitializer(new MyContextDataInitializer());
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
}
http://code.msdn.microsoft.com/Loop-Reference-handling-in-caaffaf7
http://code.msdn.microsoft.com/Loop-Reference-handling-in-caaffaf7
#5
3
You must set Preserving Object References:
必须设置保存对象引用:
var jsonSerializerSettings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
};
http://www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm
http://www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm
#1
130
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 Error Self referencing loop detected for type
JSON。为类型检测到的净错误自引用循环
it also referes to the Json.NET codeplex page at:
它还引用Json。净codeplex上页面:
http://json.codeplex.com/discussions/272371
http://json.codeplex.com/discussions/272371
Documentation: ReferenceLoopHandling setting
文档:ReferenceLoopHandling设置
#2
33
The fix is to ignore loop references and not to serialize them. This behaviour is specified in JsonSerializerSettings
.
修复方法是忽略循环引用,而不是序列化它们。此行为在JsonSerializerSettings中指定。
Single JsonConvert
with an overload:
带有重载的单个JsonConvert:
JsonConvert.SerializeObject((from a in db.Events where a.Active select a).ToList(), Formatting.Indented,
new JsonSerializerSettings() {
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
}
);
If you'd like to make this the default behaviour, add a Global Setting with code in Application_Start()
in Global.asax.cs:
如果您想让这成为默认行为,请在Global.asax.cs .cs .中添加一个带有代码的Application_Start()全局设置:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings {
Formatting = Newtonsoft.Json.Formatting.Indented,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
};
Reference: https://github.com/JamesNK/Newtonsoft.Json/issues/78
参考:https://github.com/JamesNK/Newtonsoft.Json/issues/78
#3
14
If using ASP.NET Core MVC, add this to the ConfigureServices method of your startup.cs file:
如果使用ASP。NET Core MVC,将它添加到启动的ConfigureServices方法中。cs文件:
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
#4
12
This may help you.
这可能会帮助你。
public MyContext() : base("name=MyContext")
{
Database.SetInitializer(new MyContextDataInitializer());
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
}
http://code.msdn.microsoft.com/Loop-Reference-handling-in-caaffaf7
http://code.msdn.microsoft.com/Loop-Reference-handling-in-caaffaf7
#5
3
You must set Preserving Object References:
必须设置保存对象引用:
var jsonSerializerSettings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
};
http://www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm
http://www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm