如何修复循环引用错误处理时使用JSON [英] How To Fix Circular Reference Error When Dealing With Json

查看:156
本文介绍了如何修复循环引用错误处理时使用JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是我原来的职位的一部分,这里的Get进入EXTJS的GridPanel 数据

This question is a part of my original post here Get Data Into Extjs GridPanel

下面是我的控制器,从SQL数据库中读取数据,然后我试图连接code JSON作为结果和数据发送回我gridview.js

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

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();

    } 

下面就是问题所在,
上述code,我让我的GridView表无数据执行gridview.js的时候,但如果我直接进入我的控制器的方法,像这样

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

http://localhost:55099/GridView/writeRecord

我得到这个错误,

I get this error,

而序列类型'System.Globalization.CultureInfo的对象时​​检测到循环引用。
  说明当前Web请求的执行过程中发生未处理的异常。请查看有关错误的详细信息的堆栈跟踪以及它起源于code。
  异常详细信息: System.InvalidOperationException:在序列化类型的对象System.Globalization.CultureInfo中检测到循环引用

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

有人可以帮助..

推荐答案

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

I use the following tool for serializing and deserializing JSON:

<一个href=\"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.

另外从newtonking JSON.NET速度极快。

Also json.net from newtonking is extremely fast.

其他选项是使用DTO的和潜水员通过提到映射Automapper他们。

The other options is to use DTO's and map them via Automapper as mentioned by Diver.

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

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'
        }    
});  

这篇关于如何修复循环引用错误处理时使用JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆