在处理Json时如何解决循环引用错误 [英] How To Fix Circular Reference Error When Dealing With Json

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

问题描述

这个问题是我原来的帖子的一部分获取数据到Extjs GridPanel

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

下面是我的Controller从sql数据库读取数据,然后我试图对结果进行编码作为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();

    } 

这里是问题,
与上述代码,当我执行gridview.js时,我得到没有数据的gridview表,但是如果我直接访问我的控制器这样的方法

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

我收到这个错误,


在序列化System.Globalization.CultureInfo类型的对象时检测到循环引用。
说明:在执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误的更多信息及其在代码中的位置。
异常详细信息: 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:

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

它忽略循环引用。

另外来自newtonking的json.net是非常快的。

Also json.net from newtonking is extremely fast.

其他选项是使用DTO,并通过Diver的提示通过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天全站免登陆