C# Json 序列化类型对象时检测到循环引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14592781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Json A circular reference was detected while serializing an object of type
提问by Expert wanna be
Give the classes:
给班级:
public class Parent
{
public int id {get; set;}
public int name {get; set;}
public virtual ICollection<Child> children {get; set;}
}
[Table("Child")]
public partial class Child
{
[Key]
public int id {get; set;}
public string name { get; set; }
[NotMapped]
public string nickName { get; set; }
}
And the controller code:
和控制器代码:
List<Parent> parents = parentRepository.Get();
return Json(parents);
It works on LOCALHOST, but it does not work on live server:
它适用于 LOCALHOST,但不适用于实时服务器:
ERROR : Json A circular reference was detected while serializing an object of type
错误:Json 序列化类型对象时检测到循环引用
I did a search and found the [ScriptIgnore]
attribute, so I changed the model to
我搜索了一下,找到了[ScriptIgnore]
属性,于是把模型改成了
using System.Web.Script.Serialization;
public class Parent
{
public int id {get; set;}
public int name {get; set;}
[ScriptIgnore]
public virtual ICollection<Child> children {get; set;}
}
But the same error occur on live server (win2008).
但是在实时服务器(win2008)上发生了同样的错误。
How can I avoid that error and serialize the parent data successfully?
如何避免该错误并成功序列化父数据?
采纳答案by RMalke
Try the following code:
试试下面的代码:
return Json(
parents.Select(x => new {
id = x.id,
name = x.name,
children = x.children.Select(y => new {
// Assigment of child fields
})
}));
...or if you only need the parent properties:
...或者如果您只需要父属性:
return Json(
parents.Select(x => new {
id = x.id,
name = x.name
}));
It is not really the solution for the problem, but it is a common workaround when serializing DTOs...
这并不是问题的真正解决方案,但它是序列化 DTO 时的常见解决方法......
回答by David
I had a similar issue and likewise i was not able to resolve the underlying issue. I figure the server is using a dll different from localhost for the conversion to json via json.encode.
我有一个类似的问题,同样我无法解决根本问题。我认为服务器正在使用与本地主机不同的 dll 通过 json.encode 转换为 json。
I did post the question and my resolution here A circular reference was detected while serializing with Json.Encode
我确实在这里发布了问题和我的解决方案使用 Json.Encode 进行序列化时检测到循环引用
I resolved with mvchelper.
我用 mvchelper 解决了。
回答by A.Kosecik
I'm Using the fix, Because Using Knockout in MVC5 views.
我正在使用修复程序,因为在 MVC5 视图中使用 Knockout。
On action
在行动
return Json(ModelHelper.GetJsonModel<Core_User>(viewModel));
function
功能
public static TEntity GetJsonModel<TEntity>(TEntity Entity) where TEntity : class
{
TEntity Entity_ = Activator.CreateInstance(typeof(TEntity)) as TEntity;
foreach (var item in Entity.GetType().GetProperties())
{
if (item.PropertyType.ToString().IndexOf("Generic.ICollection") == -1 && item.PropertyType.ToString().IndexOf("SaymenCore.DAL.") == -1)
item.SetValue(Entity_, Entity.GetPropValue(item.Name));
}
return Entity_;
}
回答by Mohsen Rahimi
You could use this code and do not use select Extention function to filter your column.
您可以使用此代码并且不要使用 select Extention 函数来过滤您的列。
var list = JsonConvert.SerializeObject(Yourmodel,
Formatting.None,
new JsonSerializerSettings() {
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
});
return list;