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

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

问题描述

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

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

下面是我的控制器,它从 sql db 读取数据,然后我尝试将结果编码为 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天全站免登陆