使用MVC连载JSON Json.NET [英] MVC serialize json using Json.NET

查看:108
本文介绍了使用MVC连载JSON Json.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code一个控制器内

I have this code inside a Controller

[HttpPost]
public ActionResult Index([DataSourceRequest]DataSourceRequest request)
{
    var tickets = db.Tickets.Include(t => t.AreaOfBusiness).Include(t => t.Assignee).Include(t => t.Severity).Include(t => t.TicketStatu);
    return this.Json(tickets.ToDataSourceResult(request));
}

但我得到

而序列化类型的对象时检测到循环引用'System.Data.Entity.DynamicProxies.Ticket_2B104FE45830306408DA130C08090F61ADA6B8A0106592FECE85087B94B

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Ticket_2B104FE45830306408DA130C08090F61ADA6B8A0106592FECE85087B94B

启动KendoGrid时。

when launching KendoGrid.

我读到NewtonSoft Json.Net可以处理循环引用。我不明白如何使用它。

I read that NewtonSoft Json.Net can handle Circular Reference. I cannot understand how to use it.

您可以帮我修改code,以使用Json.Net?

Can you help me modify the code in order to use the Json.Net?

感谢名单提前!

推荐答案

或...您可以启用json.net功能序列化处理的对象循环引用

Or... you can enable json.net feature to serialize handling circular references for objects

在您的的global.asax.cs

System.Net.Http.Formatting.JsonMediaTypeFormatter jsonMediaTypeFormatter = GlobalConfiguration.Configuration.Formatters.OfType<System.Net.Http.Formatting.JsonMediaTypeFormatter>().FirstOrDefault();
jsonMediaTypeFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
jsonMediaTypeFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;

记住禁用延迟加载,否则将下载整个数据库,使用包括dbsets让你想要的关系
现在来分析这将像一本字典的JSON,这里举例: https://gist.github.com / keesey / 7995398

这篇关于使用MVC连载JSON Json.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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