在序列化类型的对象时检测到循环引用

时间:2023-01-19 20:45:10

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:

它可以在本地主机上工作,但不能在实时服务器上工作:

ERROR : Json A circular reference was detected while serializing an object of type

错误:在序列化类型的对象时检测到一个循环引用

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).

但是在live server上也发生了相同的错误(win2008)。

How can I avoid that error and serialize the parent data successfully?

如何避免这个错误并成功序列化父数据?

4 个解决方案

#1


47  

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时,它是一个常见的解决方案。

#2


2  

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.

我也遇到过类似的问题,同样的,我也无法解决根本的问题。我认为服务器正在使用一个与localhost不同的dll通过json.encode进行转换。

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解决。

#3


2  

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;

#4


0  

I'm Using the fix, Because Using Knockout in MVC5 views.

我使用的是fix,因为在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_;  
    }

#1


47  

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时,它是一个常见的解决方案。

#2


2  

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.

我也遇到过类似的问题,同样的,我也无法解决根本的问题。我认为服务器正在使用一个与localhost不同的dll通过json.encode进行转换。

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解决。

#3


2  

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;

#4


0  

I'm Using the fix, Because Using Knockout in MVC5 views.

我使用的是fix,因为在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_;  
    }