如何在处理Json时修复循环引用错误

这个问题是我在这里的原始post的一部分获取数据到Extjs GridPanel

下面是我的控制器从sql db读取数据然后我试图将结果编码为JSON并将数据发送回我的gridview.js

public JsonResult writeRecord() //public string writeRecord() { Response.Write("Survey Completed!"); SqlConnection conn = DBTools.GetDBConnection("ApplicationServices2"); string sqlquery = "SELECT Q1, Q2, Q3, Q4, Improvements, Comments FROM myTable"; SqlDataAdapter cmd = new SqlDataAdapter(sqlquery, conn); DataSet myData = new DataSet(); cmd.Fill(myData, "myTable"); conn.Open(); conn.Close(); return Json(myData, JsonRequestBehavior.AllowGet); //return myData.GetXml(); } 

这就是问题所在,使用上面的代码,我在执行gridview.js时得到的gridview表没有数据,但如果我直接访问我的控制器的方法就像这样

 http://localhost:55099/GridView/writeRecord 

我收到这个错误,

序列化“System.Globalization.CultureInfo”类型的对象时检测到循环引用。 描述:执行当前Web请求期间发生未处理的exception。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 exception详细信息: System.InvalidOperationException:序列化“System.Globalization.CultureInfo”类型的对象时检测到循环引用。

有人可以帮忙..

我使用以下工具来序列化和反序列化JSON:

http://james.newtonking.com/pages/json-net.aspx

它非常易于使用且非常轻便。

在序列化时,我们使用此选项:

 JsonConvert.SerializeObject(myObject, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }) 

它忽略了循环引用。

来自newtonking的json.net也非常快。

其他选项是使用DTO并通过Diver提到的Automapper进行映射。

编辑:我怀疑你的商店是错的:

 var store = Ext.create('Ext.data.JsonStore', { storeId: 'myData', reader: new Ext.data.JsonReader({ root: 'myTable', fields: [{ name: 'Q1', type: 'int' }, { name: 'Q2', type: 'int' }, { name: 'Q3', type: 'int' }, { name: 'Q4', type: 'int' }, { name: 'Q5', type: 'int' }, { name: 'Improvements', type: 'string' }, { name: 'Comments', type: 'string'}] }), proxy: { type: 'json', url: 'GridView/writeRecord' } }); 

这是因为CultureInfo中的某些内容具有对自身的引用(此类型),并且在转换为JSON的过程中它失败了。 要避免这种情况,您应该使用ViewModel(仅返回客户端所需的信息)。 你可以在这里阅读更多http://blogs.msdn.com/b/dphill/archive/2009/01/31/the-viewmodel-pattern.aspx

在您的情况下,您应该为您的数据创建ViewModel,将您的数据转换为这些数据类型并将它们返回转换为JSON。 为了从Model转换为ViewModel,请考虑使用AutoMapper http://automapper.codeplex.com/或类似的工具。