解决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 ...
随机推荐
- WEB工程数据库相关安装脚本写作
1. 数据库oracle安装 2. 数据库用户创建,表空间创建,表创建 #!/bin/bash current_path=`pwd` create_tablespace=${current_path} ...
- Javascript 层次
1. HTML5, Tool, Framework ---------------------------UI: Liger UI, jQuery UI, jQuery Mobile -------- ...
- UIPickerView 地区解析 -- 全国省、市、区 plist 解析 -- 读取UIPickerView 当前显示内容
一个简单的plist 解析过程,借助UIPickerView 实现了手选全国的 省市区 方法, 源码中有详细注释:长句自己可以拆开看,最好的方法是,拆开,并打印,查看每一步打印的结果,结合Plist文 ...
- QT中关闭应用程序和窗口的函数(quit(),exit()以及close()的区别)
使用QT编辑界面,其中带来很大方便的一点就是Qt中自带丰富的.种类齐全的类及其功能函数,程序员可以在编辑程序的过程中简单地直接调用.关于窗口关闭的操作,在这里指出常用的三个槽,即quit(),exit ...
- Caption,Text,WindowText的区别——TControl也有FText,是为了模拟一个窗口
TControl = class(TComponent) // 控件的Windows功能从TControl开始 property Caption: TCaption read GetText writ ...
- hiho #1044 : 状态压缩·一
描述 小Hi和小Ho在兑换到了喜欢的奖品之后,便继续起了他们的美国之行,思来想去,他们决定乘坐火车前往下一座城市——那座城市即将举行美食节! 但是不幸的是,小Hi和小Ho并没有能够买到很好的火车票—— ...
- -_-#【Mac】MacVim
MacVim安装与配置 Mac开发利器之程序员编辑器MacVim学习总结 Git时代的VIM不完全使用教程 zencoding-vim This repository has moved to htt ...
- vector::erase returns incompatible iterator in debug build
关于std::vector中erase的用法http://www.cplusplus.com/reference/vector/vector/erase/ #include <vector> ...
- 字符串查找 strstr
strstr函数 分类: LINUX 函数名: strstr 功 能: 在串中查找指定字符串的第一次出现 用 法: char *strstr(char *str1, char *str2); st ...
- poj 1556 The door
题目链接:http://poj.org/problem?id=1556 #include<cstdio> #include<cstring> #include<cmath ...