C# 处理Json时如何修复循环引用错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12584986/
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
How To Fix Circular Reference Error When Dealing With Json
提问by EagleFox
This question is a part of my original post here Get Data Into Extjs GridPanel
这个问题是我在Get Data Into Extjs GridPanel 中的原始帖子的一部分
Below is my Controller that reads data from sql db and then I am trying to encode the result as JSON and send the data back to my gridview.js
下面是我从 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();
}
Here lies the problem, with the above code, I get my gridview table with no data when executing gridview.js, but if I directly access my controller's method like this
这就是问题所在,使用上面的代码,我在执行 gridview.js 时得到了没有数据的 gridview 表,但是如果我像这样直接访问控制器的方法
http://localhost:55099/GridView/writeRecord
I get this error,
我收到这个错误,
A circular reference was detected while serializing an object of type 'System.Globalization.CultureInfo'.Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details:System.InvalidOperationException: A circular reference was detected while serializing an object of type 'System.Globalization.CultureInfo'.
序列化“System.Globalization.CultureInfo”类型的对象时检测到循环引用。说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。 异常详细信息:System.InvalidOperationException:在序列化“System.Globalization.CultureInfo”类型的对象时检测到循环引用。
Can someone please help..
有人可以帮忙吗..
采纳答案by Johan Haest
I use the following tool for serializing and deserializing JSON:
我使用以下工具来序列化和反序列化 JSON:
http://james.newtonking.com/pages/json-net.aspx
http://james.newtonking.com/pages/json-net.aspx
It's very easy to use and very lightweight.
它非常易于使用且非常轻巧。
While serializing we use this option:
在序列化时我们使用这个选项:
JsonConvert.SerializeObject(myObject, Formatting.Indented,
new JsonSerializerSettings {
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
})
It ignores circular references.
它忽略循环引用。
Also json.net from newtonking is extremely fast.
来自 newtonking 的 json.net 也非常快。
The other options is to use DTO's and map them via Automapper as mentioned by Diver.
另一种选择是使用 DTO 并通过 Diver 提到的 Automapper 映射它们。
Edit: I suspect your store is wrong:
编辑:我怀疑你的商店是错误的:
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'
}
});
回答by berliner
It's because something inside CultureInfo has a reference to itself (this type) and in process of converting to JSON it fails. To avoid this situation, you should use ViewModels (return to client only information that is needed). You can read more here http://blogs.msdn.com/b/dphill/archive/2009/01/31/the-viewmodel-pattern.aspx
这是因为 CultureInfo 中的某些内容具有对自身的引用(这种类型),并且在转换为 JSON 的过程中失败了。为避免这种情况,您应该使用 ViewModels(仅将需要的信息返回给客户端)。你可以在这里阅读更多 http://blogs.msdn.com/b/dphill/archive/2009/01/31/the-viewmodel-pattern.aspx
In your situation you shoud create ViewModels for your data, convert your data to these data types and return them converted to JSON. For the purpose of converting from Model to ViewModel consider using AutoMapper http://automapper.codeplex.com/or some similar tool.
在您的情况下,您应该为您的数据创建 ViewModel,将您的数据转换为这些数据类型并将它们转换为 JSON。为了从 Model 转换为 ViewModel,请考虑使用 AutoMapper http://automapper.codeplex.com/或一些类似的工具。

