解决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());
}
}
}
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
};
}
}
step4.向平时一样使用就可以了
//获取列表
public JsonResult GetList()
{
List<student> list = _Context.students.Where(q => q.sno == "").ToList();
//方法1
return Json(list);
//方法2
//return new JsonNetResult() {
// Data=list
//};
}
获取的结果,说明,这种方式指定忽略循环引用,是在指定循环级数后忽略,返回的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的更多相关文章
- 解决MVC中Model上的特性在EF框架刷新时清空的问题
MVC中关于前端数据的效验一般都是通过在Model中相关的类上打上特性来实现. 但是在我们数据库发生改变,EF框架需要刷新时会把我们在Model上的特性全部清除,这样的话,我们前端的验证就会失效. 因 ...
- 未能加载文件或程序集 Newtonsoft.Json, Version=4.5.0.0 的报错,解决方法
使用httpclient测试webapi的时候客户端报错: {"未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, P ...
- .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程
JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...
- Newtonsoft—Json.NET常用方法简述
Json.NET常用方法汇总(可解决日常百分之90的需求) 0.Json.NET基础用法 首先去官网下载最新的Newtonsoft.Json.dll(也可以使用VS自带的NuGet搜索Json.NET ...
- Newtonsoft.Json(Json.net)的基本用法
Newtonsoft.Json(Json.net)的基本用法 其它资料 JSON详解 添加引用: 使用NuGet,命令:install-package Newtonsoft.Json 实体类: pub ...
- Newtonsoft.Json 的基本用法
Ø 前言 说起 C# 对 JSON 的操作(序列化与反序列化),大家都会想到 JavaScriptSerializer.DataContractJsonSerializer 与 Newtonsoft ...
- Newtonsoft.Json[C#]
C# Newtonsoft.Json JsonSerializerSettings配置序列化操作 https://blog.csdn.net/u011127019/article/details/72 ...
- Newtonsoft.Json高级篇:TypeNameHandling设置
原文:Newtonsoft.Json高级篇:TypeNameHandling设置 此示例使用TypeNameHandling 设置在序列化JSON和读取类型信息时包含类型信息,以便在反序列化JSON时 ...
- 关于Newtonsoft.Json引用报错
自己运行的vs版本是2012,然后同事用了2017的,我把代码发给他后运行发现报以下错误: {未能加载文件或程序集"Newtonsoft.Json, Version=4.5.0.0, Cul ...
- C# 中Newtonsoft.Json的安装和使用
官网参考:http://json.codeplex.com/ 在程序包管理控制台,键入NuGet命令 install-package Newtonsoft.Json 安装Newtonsoft.Js ...
随机推荐
- 最简单理解CGI,FastCGI,WSGI
CGI规定了如何执行本地脚本技术规范(协议),FastCGI规定了如何远程请求执行脚本的技术规范,WSGI规定了如何请求执行Python脚本的规范. 他们的相同点就是envionment variab ...
- frameset iframe用来分页
frameset用来分大的框架 iframe用来在frame分框架之后,内嵌分割. <FRAMESET border=1 frameSpacing=1 borderColor=#47478d r ...
- BZOJ 1200 木梳
Description Input 第一行为整数L,其中4≤L≤100000,且有50%的数据满足L≤104,表示木板下侧直线段的长.第二行为L个正整数A1,A2,…,AL,其中Ai≤108 Outp ...
- UBUNTU下FPT工具--lftp使用说明
lftp 是UBUNTU下一个功能强大的下载工具,它支持访问文件的协议: ftp, ftps, http, https, hftp, fish.(其中ftps 和https需要在编译的时候包含open ...
- github继续折腾
又在折腾github了,本来以前在neworiginou这个github上上传过项目的: 现在想在另一个github上joely上传项目,发现按以前的流程做个测试,居然没能上传成功! 经发现,以前的n ...
- 数据结构(动态树):COGS 27. [WC 2006] 水管局长
27. [WC 2006] 水管局长 ★★★☆ 输入文件:tube.in 输出文件:tube.out 简单对比时间限制:3 s 内存限制:128 MB [问题描述 ] SC 省 MY ...
- 杂题 SPOJ MOBILE2 - Mobiles
MOBILE2 - Mobiles no tags You have been asked to buy a gift for your baby brother, Ike. However, yo ...
- ISAP 模板
#include <iostream> #include <cstring> #include <cstdio> #include <queue> us ...
- 【字符串】【最小表示法】Vijos P1683 有根树的同构问题
题目链接: https://vijos.org/p/1683 题目大意: 给M棵树,每棵N个点,N-1条边,树边有向,问哪些树同构. 题目思路: [字符串][最小表示法] 用()表示一个节点,那么三个 ...
- HashMap,TreeMap,LinkedHashMap学习
Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复.Hashmap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快 ...