解决MVC Json序列化的循环引用问题/EF Json序列化循引用问题---Newtonsoft.Json
1..Net开源Json序列化工具Newtonsoft.Json中提供了解决序列化的循环引用问题:
方式1:指定Json序列化配置为 ReferenceLoopHandling.Ignore
方式2:指定 JsonIgnore忽略 引用对象
实例1,解决MVC的Json序列化引用方法:
step1:在项目上添加引用 Newtonsoft.Json程序包,命令:Insert-Package Newtonsoft.Json
step2:在项目中添加一个类,继承JsonResult,代码如下:
/// <summary> /// 继承JsonResut,重写序列化方式 /// </summary> public class JsonNetResult : JsonResult {public JsonSerializerSettings Settings { get; private set; }public JsonNetResult(){Settings = new JsonSerializerSettings{//这句是解决问题的关键,也就是json.net官方给出的解决配置选项. ReferenceLoopHandling = ReferenceLoopHandling.Ignore};}public override void ExecuteResult(ControllerContext context){if (context == null)throw new ArgumentNullException("context");if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))throw new InvalidOperationException("JSON GET is not allowed");HttpResponseBase response = context.HttpContext.Response;response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;if (this.ContentEncoding != null)response.ContentEncoding = this.ContentEncoding;if (this.Data == null)return;var scriptSerializer = JsonSerializer.Create(this.Settings);using (var sw = new StringWriter()){scriptSerializer.Serialize(sw, this.Data);response.Write(sw.ToString());}} }
View Code
step3:在项目添加BaseController,重写Json()方法,代码如下:
public class BaseController : Controller {public StudentContext _Context = new StudentContext();/// <summary>/// 重写,Json方法,使之返回JsonNetResult类型/// </summary>protected override JsonResult Json(object data, string contentType,Encoding contentEncoding, JsonRequestBehavior behavior){return new JsonNetResult{Data = data,ContentType = contentType,ContentEncoding = contentEncoding,JsonRequestBehavior = behavior};} }
View Code
step4.向平时一样使用就可以了
//获取列表 public JsonResult GetList() {List<student> list = _Context.students.Where(q => q.sno == "103").ToList();//方法1return Json(list);//方法2//return new JsonNetResult() {// Data=list//}; }
View Code
获取的结果,说明,这种方式指定忽略循环引用,是在指定循环级数后忽略,返回的json数据中还是有部分循环的数据
解决EF Json序列化循环引用方法2,在指定的关联对象上,添加JsonIgnore 方法注释
[JsonIgnore] public virtual ICollection<score> scores { get; set; }
返回结果中,没有关联表数据
相关文章:
Newtonsoft.Json简介:http://blog.csdn.net/u011127019/article/details/51706619
EF的Json序列化,循环引用:http://blog.csdn.net/u011127019/article/details/51706659
自引用:https://my.oschina.net/tianma3798/blog/673159
解决MVC Json序列化的循环引用问题/EF Json序列化循引用问题---Newtonsoft.Json相关推荐
- Newtonsoft.Json序列化库
Unity自己的Json序列化是不支持字典格式的,而且功能比较单一,这里介绍一个.Net中开源的Json序列化和反序列化库和基本用法以及常用的数据处理方法(github地址:https://githu ...
- 未能加载文件或程序集 Newtonsoft.Json, Version=4.5.0.0 的报错,解决方法
未能加载文件或程序集 Newtonsoft.Json, Version=4.5.0.0 的报错,解决方法 参考文章: (1)未能加载文件或程序集 Newtonsoft.Json, Version=4. ...
- Newtonsoft.Json(Json.net)的基本用法
Newtonsoft.Json(Json.net)的基本用法 添加引用: 使用NuGet,命令:install-package Newtonsoft.Json 实体类: public class Bo ...
- C# Newtonsoft.Json 高级用法
Newtonsoft.Json介绍: 做Web开发的,没有接触过JavaScript的肯定很少,做前端开发,没有接触过Ajax的估计更不多了.现在的系统大多数是分布式系统,分布式系统就会涉及到数据的传 ...
- 巧用Newtonsoft.Json处理重复请求/并发请求?
背景 一些用户请求在某些情况下是可能重复发送的,如果是查询类操作并无大碍,但其中有些涉及写入操作,一旦重复了,可能会导致很严重的后果.例如交易接口如果重复请求,可能会重复下单. 问题 假设我们把请求参 ...
- Newtonsoft.Json类库学习
目录 介绍... 1 Newtonsoft.Json下类... 1 Newtonsoft.Json.JsonConvert. 1 Newtonsoft.Json.JsonConvert.Seriali ...
- 类型“JsonConvert”同时存在于“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6a
类型"JsonConvert"同时存在于"Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToke ...
- C#安装Newtonsoft.Json并调用
Newtonsoft.Json官网: Json.NET - Newtonsoft 首先安装Newtonsoft.Json 1.项目->管理NuGet程序包 2.下载Newtonsoft.Json ...
- 在unity中添加Newtonsoft.Json
在Packages->packages-lock.json中修改com.unity.collab-proxy为 "com.unity.collab-proxy": { ...
最新文章
- bamboo php,建立数据模型 — asbamboo php framework 文档
- block用法(转)
- 公式没有编号_知乎公式编辑器的一些小技巧 amp; 使用规范
- How to make Windows Form app truly Full Screen (and to hide Taskbar) in C#? 转
- linux 的 usr 文件
- ASP.NET Core中使用表达式树创建URL
- C++成员函数重载、覆盖和隐藏的区别
- 实现多线程的几种方式
- 目标检测——知识蒸馏的学习笔记
- svn访问方式 linux,ssh+svn 方式访问svn
- python 通过索引迭代列表_python – NumPy – 迭代2D列表和打印(行,列)索引
- 检测UDP端口是否开放的命令
- RHadoop安装和使用
- ubuntu16.04便捷使用(常用工具、常用快捷键、常用使用教程)
- html文字边框颜色,css背景颜色/文字/边框 设置渐变色
- 谷歌浏览器搜索框记录_如何清除您的Google搜索记录
- 锤子pro2 Android8,锤子 坚果Pro2 MIUI10 Android N 快如闪电 指纹支付 完美ROOT
- android 设备注册,Android平台上PMEM的使用及Platform设备注册(二)
- linux查看端口pvid,关于PVID的几个疑问
- 以太网技术(地址静态分配和动态分配)
热门文章
- IE下Ajax 提交中文乱码问题
- 零售流通ERP系统——基础信息的确立与实施
- 矩阵对抗与系统补丁200911(第2期)下载
- 51cto博客程序错误
- 多层科目任意组合汇总报表的性能优化 (上)
- 【Java 并发】详解 ThreadLocal
- Office 365管理员指引 17——Sharepoint 讨论版
- Nginx搭建flv视频点播服务器
- ch5 MySQL 备份与恢复
- Windows开发环境搭建(安装 VS2010, VS2013, VS2015 Community, Windows Server 2008 R2)