序列化对象时检测到循环引用。这个错误到底是什么?
我想将 List
对象序列化为 JSON 数据。
我当前在 ASP.NET MVC 应用程序中使用的代码是:
List<Question> questionList;
questionList = questionManager.GetquestionsByTestId(id);
var listData = questionList.ToArray();
JavaScriptSerializer serializer = new JavaScriptSerializer();
string strData = serializer.Serialize(listData);
JsonResult json = Json(strData, JsonRequestBehavior.AllowGet);
该代码似乎会导致以下错误:
A circular reference was detected while serializing an object of type 'TestEnvironment.Models.Question'.
此错误意味着什么?
编辑:问题类
public partial class Question : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _QuestionId;
private string _QuestionText;
private System.Nullable<int> _TopicId;
private System.Nullable<int> _Marks;
private System.Nullable<int> _QuestionTypeId;
private System.Nullable<int> _ChapterId;
private System.Nullable<int> _Weightage;
private System.Nullable<System.TimeSpan> _ExpectedTimeToAnswer;
private EntitySet<Answer> _Answers;
private EntitySet<TestDetail> _TestDetails;
private EntityRef<Chapter> _Chapter;
private EntityRef<QuestionType> _QuestionType;
private EntityRef<Topic> _Topic;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnQuestionIdChanging(int value);
partial void OnQuestionIdChanged();
partial void OnQuestionTextChanging(string value);
partial void OnQuestionTextChanged();
partial void OnTopicIdChanging(System.Nullable<int> value);
partial void OnTopicIdChanged();
partial void OnMarksChanging(System.Nullable<int> value);
partial void OnMarksChanged();
partial void OnQuestionTypeIdChanging(System.Nullable<int> value);
partial void OnQuestionTypeIdChanged();
partial void OnChapterIdChanging(System.Nullable<int> value);
partial void OnChapterIdChanged();
partial void OnWeightageChanging(System.Nullable<int> value);
partial void OnWeightageChanged();
partial void OnExpectedTimeToAnswerChanging(System.Nullable<System.TimeSpan> value);
partial void OnExpectedTimeToAnswerChanged();
#endregion
public Question()
{
this._Answers = new EntitySet<Answer>(new Action<Answer>(this.attach_Answers), new Action<Answer>(this.detach_Answers));
this._TestDetails = new EntitySet<TestDetail>(new Action<TestDetail>(this.attach_TestDetails), new Action<TestDetail>(this.detach_TestDetails));
this._Chapter = default(EntityRef<Chapter>);
this._QuestionType = default(EntityRef<QuestionType>);
this._Topic = default(EntityRef<Topic>);
OnCreated();
}
[Column(Storage="_QuestionId", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int QuestionId
{
get
{
return this._QuestionId;
}
set
{
if ((this._QuestionId != value))
{
this.OnQuestionIdChanging(value);
this.SendPropertyChanging();
this._QuestionId = value;
this.SendPropertyChanged("QuestionId");
this.OnQuestionIdChanged();
}
}
}
[Column(Storage="_QuestionText", DbType="NVarChar(MAX)")]
public string QuestionText
{
get
{
return this._QuestionText;
}
set
{
if ((this._QuestionText != value))
{
this.OnQuestionTextChanging(value);
this.SendPropertyChanging();
this._QuestionText = value;
this.SendPropertyChanged("QuestionText");
this.OnQuestionTextChanged();
}
}
}
[Column(Storage="_TopicId", DbType="Int")]
public System.Nullable<int> TopicId
{
get
{
return this._TopicId;
}
set
{
if ((this._TopicId != value))
{
if (this._Topic.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnTopicIdChanging(value);
this.SendPropertyChanging();
this._TopicId = value;
this.SendPropertyChanged("TopicId");
this.OnTopicIdChanged();
}
}
}
[Column(Storage="_Marks", DbType="Int")]
public System.Nullable<int> Marks
{
get
{
return this._Marks;
}
set
{
if ((this._Marks != value))
{
this.OnMarksChanging(value);
this.SendPropertyChanging();
this._Marks = value;
this.SendPropertyChanged("Marks");
this.OnMarksChanged();
}
}
}
[Column(Storage="_QuestionTypeId", DbType="Int")]
public System.Nullable<int> QuestionTypeId
{
get
{
return this._QuestionTypeId;
}
set
{
if ((this._QuestionTypeId != value))
{
if (this._QuestionType.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnQuestionTypeIdChanging(value);
this.SendPropertyChanging();
this._QuestionTypeId = value;
this.SendPropertyChanged("QuestionTypeId");
this.OnQuestionTypeIdChanged();
}
}
}
[Column(Storage="_ChapterId", DbType="Int")]
public System.Nullable<int> ChapterId
{
get
{
return this._ChapterId;
}
set
{
if ((this._ChapterId != value))
{
if (this._Chapter.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnChapterIdChanging(value);
this.SendPropertyChanging();
this._ChapterId = value;
this.SendPropertyChanged("ChapterId");
this.OnChapterIdChanged();
}
}
}
[Column(Storage="_Weightage", DbType="Int")]
public System.Nullable<int> Weightage
{
get
{
return this._Weightage;
}
set
{
if ((this._Weightage != value))
{
this.OnWeightageChanging(value);
this.SendPropertyChanging();
this._Weightage = value;
this.SendPropertyChanged("Weightage");
this.OnWeightageChanged();
}
}
}
[Column(Storage="_ExpectedTimeToAnswer", DbType="Time")]
public System.Nullable<System.TimeSpan> ExpectedTimeToAnswer
{
get
{
return this._ExpectedTimeToAnswer;
}
set
{
if ((this._ExpectedTimeToAnswer != value))
{
this.OnExpectedTimeToAnswerChanging(value);
this.SendPropertyChanging();
this._ExpectedTimeToAnswer = value;
this.SendPropertyChanged("ExpectedTimeToAnswer");
this.OnExpectedTimeToAnswerChanged();
}
}
}
[Association(Name="Question_Answer", Storage="_Answers", ThisKey="QuestionId", OtherKey="QuestionId")]
public EntitySet<Answer> Answers
{
get
{
return this._Answers;
}
set
{
this._Answers.Assign(value);
}
}
[Association(Name="Question_TestDetail", Storage="_TestDetails", ThisKey="QuestionId", OtherKey="QuestionId")]
public EntitySet<TestDetail> TestDetails
{
get
{
return this._TestDetails;
}
set
{
this._TestDetails.Assign(value);
}
}
[Association(Name="Chapter_Question", Storage="_Chapter", ThisKey="ChapterId", OtherKey="ChapterId", IsForeignKey=true)]
public Chapter Chapter
{
get
{
return this._Chapter.Entity;
}
set
{
Chapter previousValue = this._Chapter.Entity;
if (((previousValue != value)
|| (this._Chapter.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Chapter.Entity = null;
previousValue.Questions.Remove(this);
}
this._Chapter.Entity = value;
if ((value != null))
{
value.Questions.Add(this);
this._ChapterId = value.ChapterId;
}
else
{
this._ChapterId = default(Nullable<int>);
}
this.SendPropertyChanged("Chapter");
}
}
}
[Association(Name="QuestionType_Question", Storage="_QuestionType", ThisKey="QuestionTypeId", OtherKey="QuestionTypeId", IsForeignKey=true)]
public QuestionType QuestionType
{
get
{
return this._QuestionType.Entity;
}
set
{
QuestionType previousValue = this._QuestionType.Entity;
if (((previousValue != value)
|| (this._QuestionType.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._QuestionType.Entity = null;
previousValue.Questions.Remove(this);
}
this._QuestionType.Entity = value;
if ((value != null))
{
value.Questions.Add(this);
this._QuestionTypeId = value.QuestionTypeId;
}
else
{
this._QuestionTypeId = default(Nullable<int>);
}
this.SendPropertyChanged("QuestionType");
}
}
}
[Association(Name="Topic_Question", Storage="_Topic", ThisKey="TopicId", OtherKey="TopicId", IsForeignKey=true)]
public Topic Topic
{
get
{
return this._Topic.Entity;
}
set
{
Topic previousValue = this._Topic.Entity;
if (((previousValue != value)
|| (this._Topic.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Topic.Entity = null;
previousValue.Questions.Remove(this);
}
this._Topic.Entity = value;
if ((value != null))
{
value.Questions.Add(this);
this._TopicId = value.TopicId;
}
else
{
this._TopicId = default(Nullable<int>);
}
this.SendPropertyChanged("Topic");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void attach_Answers(Answer entity)
{
this.SendPropertyChanging();
entity.Question = this;
}
private void detach_Answers(Answer entity)
{
this.SendPropertyChanging();
entity.Question = null;
}
private void attach_TestDetails(TestDetail entity)
{
this.SendPropertyChanging();
entity.Question = this;
}
private void detach_TestDetails(TestDetail entity)
{
this.SendPropertyChanging();
entity.Question = null;
}
}
I would like to serialize the List<Question>
object to JSON data.
The code that I'm currently in my ASP.NET MVC application using is:
List<Question> questionList;
questionList = questionManager.GetquestionsByTestId(id);
var listData = questionList.ToArray();
JavaScriptSerializer serializer = new JavaScriptSerializer();
string strData = serializer.Serialize(listData);
JsonResult json = Json(strData, JsonRequestBehavior.AllowGet);
The code seems to result in the following error:
A circular reference was detected while serializing an object of type 'TestEnvironment.Models.Question'.
what should be this error means ?
Edited: Question Class
public partial class Question : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _QuestionId;
private string _QuestionText;
private System.Nullable<int> _TopicId;
private System.Nullable<int> _Marks;
private System.Nullable<int> _QuestionTypeId;
private System.Nullable<int> _ChapterId;
private System.Nullable<int> _Weightage;
private System.Nullable<System.TimeSpan> _ExpectedTimeToAnswer;
private EntitySet<Answer> _Answers;
private EntitySet<TestDetail> _TestDetails;
private EntityRef<Chapter> _Chapter;
private EntityRef<QuestionType> _QuestionType;
private EntityRef<Topic> _Topic;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnQuestionIdChanging(int value);
partial void OnQuestionIdChanged();
partial void OnQuestionTextChanging(string value);
partial void OnQuestionTextChanged();
partial void OnTopicIdChanging(System.Nullable<int> value);
partial void OnTopicIdChanged();
partial void OnMarksChanging(System.Nullable<int> value);
partial void OnMarksChanged();
partial void OnQuestionTypeIdChanging(System.Nullable<int> value);
partial void OnQuestionTypeIdChanged();
partial void OnChapterIdChanging(System.Nullable<int> value);
partial void OnChapterIdChanged();
partial void OnWeightageChanging(System.Nullable<int> value);
partial void OnWeightageChanged();
partial void OnExpectedTimeToAnswerChanging(System.Nullable<System.TimeSpan> value);
partial void OnExpectedTimeToAnswerChanged();
#endregion
public Question()
{
this._Answers = new EntitySet<Answer>(new Action<Answer>(this.attach_Answers), new Action<Answer>(this.detach_Answers));
this._TestDetails = new EntitySet<TestDetail>(new Action<TestDetail>(this.attach_TestDetails), new Action<TestDetail>(this.detach_TestDetails));
this._Chapter = default(EntityRef<Chapter>);
this._QuestionType = default(EntityRef<QuestionType>);
this._Topic = default(EntityRef<Topic>);
OnCreated();
}
[Column(Storage="_QuestionId", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int QuestionId
{
get
{
return this._QuestionId;
}
set
{
if ((this._QuestionId != value))
{
this.OnQuestionIdChanging(value);
this.SendPropertyChanging();
this._QuestionId = value;
this.SendPropertyChanged("QuestionId");
this.OnQuestionIdChanged();
}
}
}
[Column(Storage="_QuestionText", DbType="NVarChar(MAX)")]
public string QuestionText
{
get
{
return this._QuestionText;
}
set
{
if ((this._QuestionText != value))
{
this.OnQuestionTextChanging(value);
this.SendPropertyChanging();
this._QuestionText = value;
this.SendPropertyChanged("QuestionText");
this.OnQuestionTextChanged();
}
}
}
[Column(Storage="_TopicId", DbType="Int")]
public System.Nullable<int> TopicId
{
get
{
return this._TopicId;
}
set
{
if ((this._TopicId != value))
{
if (this._Topic.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnTopicIdChanging(value);
this.SendPropertyChanging();
this._TopicId = value;
this.SendPropertyChanged("TopicId");
this.OnTopicIdChanged();
}
}
}
[Column(Storage="_Marks", DbType="Int")]
public System.Nullable<int> Marks
{
get
{
return this._Marks;
}
set
{
if ((this._Marks != value))
{
this.OnMarksChanging(value);
this.SendPropertyChanging();
this._Marks = value;
this.SendPropertyChanged("Marks");
this.OnMarksChanged();
}
}
}
[Column(Storage="_QuestionTypeId", DbType="Int")]
public System.Nullable<int> QuestionTypeId
{
get
{
return this._QuestionTypeId;
}
set
{
if ((this._QuestionTypeId != value))
{
if (this._QuestionType.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnQuestionTypeIdChanging(value);
this.SendPropertyChanging();
this._QuestionTypeId = value;
this.SendPropertyChanged("QuestionTypeId");
this.OnQuestionTypeIdChanged();
}
}
}
[Column(Storage="_ChapterId", DbType="Int")]
public System.Nullable<int> ChapterId
{
get
{
return this._ChapterId;
}
set
{
if ((this._ChapterId != value))
{
if (this._Chapter.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnChapterIdChanging(value);
this.SendPropertyChanging();
this._ChapterId = value;
this.SendPropertyChanged("ChapterId");
this.OnChapterIdChanged();
}
}
}
[Column(Storage="_Weightage", DbType="Int")]
public System.Nullable<int> Weightage
{
get
{
return this._Weightage;
}
set
{
if ((this._Weightage != value))
{
this.OnWeightageChanging(value);
this.SendPropertyChanging();
this._Weightage = value;
this.SendPropertyChanged("Weightage");
this.OnWeightageChanged();
}
}
}
[Column(Storage="_ExpectedTimeToAnswer", DbType="Time")]
public System.Nullable<System.TimeSpan> ExpectedTimeToAnswer
{
get
{
return this._ExpectedTimeToAnswer;
}
set
{
if ((this._ExpectedTimeToAnswer != value))
{
this.OnExpectedTimeToAnswerChanging(value);
this.SendPropertyChanging();
this._ExpectedTimeToAnswer = value;
this.SendPropertyChanged("ExpectedTimeToAnswer");
this.OnExpectedTimeToAnswerChanged();
}
}
}
[Association(Name="Question_Answer", Storage="_Answers", ThisKey="QuestionId", OtherKey="QuestionId")]
public EntitySet<Answer> Answers
{
get
{
return this._Answers;
}
set
{
this._Answers.Assign(value);
}
}
[Association(Name="Question_TestDetail", Storage="_TestDetails", ThisKey="QuestionId", OtherKey="QuestionId")]
public EntitySet<TestDetail> TestDetails
{
get
{
return this._TestDetails;
}
set
{
this._TestDetails.Assign(value);
}
}
[Association(Name="Chapter_Question", Storage="_Chapter", ThisKey="ChapterId", OtherKey="ChapterId", IsForeignKey=true)]
public Chapter Chapter
{
get
{
return this._Chapter.Entity;
}
set
{
Chapter previousValue = this._Chapter.Entity;
if (((previousValue != value)
|| (this._Chapter.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Chapter.Entity = null;
previousValue.Questions.Remove(this);
}
this._Chapter.Entity = value;
if ((value != null))
{
value.Questions.Add(this);
this._ChapterId = value.ChapterId;
}
else
{
this._ChapterId = default(Nullable<int>);
}
this.SendPropertyChanged("Chapter");
}
}
}
[Association(Name="QuestionType_Question", Storage="_QuestionType", ThisKey="QuestionTypeId", OtherKey="QuestionTypeId", IsForeignKey=true)]
public QuestionType QuestionType
{
get
{
return this._QuestionType.Entity;
}
set
{
QuestionType previousValue = this._QuestionType.Entity;
if (((previousValue != value)
|| (this._QuestionType.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._QuestionType.Entity = null;
previousValue.Questions.Remove(this);
}
this._QuestionType.Entity = value;
if ((value != null))
{
value.Questions.Add(this);
this._QuestionTypeId = value.QuestionTypeId;
}
else
{
this._QuestionTypeId = default(Nullable<int>);
}
this.SendPropertyChanged("QuestionType");
}
}
}
[Association(Name="Topic_Question", Storage="_Topic", ThisKey="TopicId", OtherKey="TopicId", IsForeignKey=true)]
public Topic Topic
{
get
{
return this._Topic.Entity;
}
set
{
Topic previousValue = this._Topic.Entity;
if (((previousValue != value)
|| (this._Topic.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Topic.Entity = null;
previousValue.Questions.Remove(this);
}
this._Topic.Entity = value;
if ((value != null))
{
value.Questions.Add(this);
this._TopicId = value.TopicId;
}
else
{
this._TopicId = default(Nullable<int>);
}
this.SendPropertyChanged("Topic");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void attach_Answers(Answer entity)
{
this.SendPropertyChanging();
entity.Question = this;
}
private void detach_Answers(Answer entity)
{
this.SendPropertyChanging();
entity.Question = null;
}
private void attach_TestDetails(TestDetail entity)
{
this.SendPropertyChanging();
entity.Question = this;
}
private void detach_TestDetails(TestDetail entity)
{
this.SendPropertyChanging();
entity.Question = null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它的意思几乎就是它所说的。
循环引用意味着类在可序列化属性中具有对其自身的引用。
我猜你的问题类有一个问题类型的属性?
不幸的是,JSON 序列化器无法序列化它。
您可以尝试编写自己的序列化器,但最简单的解决方案是将该属性更改为不同的类。
如果这不正确,请在您的问题中发布您的问题类的代码。
It means pretty much what it says.
A circular reference means that a class has a reference to itself within the serializable properties.
I'm guessing that your Question-class has a property of type Question?
The JSON-Serializer is not able to serialize this unfortunately..
You could attempt to write your own serializer, but the simplest solution would be to change that property to be a different class.
If this is not correct, please post the code for your Question-class in your question.