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的更多相关文章

  1. 解决MVC中Model上的特性在EF框架刷新时清空的问题

    MVC中关于前端数据的效验一般都是通过在Model中相关的类上打上特性来实现. 但是在我们数据库发生改变,EF框架需要刷新时会把我们在Model上的特性全部清除,这样的话,我们前端的验证就会失效. 因 ...

  2. 未能加载文件或程序集 Newtonsoft.Json, Version=4.5.0.0 的报错,解决方法

    使用httpclient测试webapi的时候客户端报错: {"未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, P ...

  3. .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程

    JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...

  4. Newtonsoft—Json.NET常用方法简述

    Json.NET常用方法汇总(可解决日常百分之90的需求) 0.Json.NET基础用法 首先去官网下载最新的Newtonsoft.Json.dll(也可以使用VS自带的NuGet搜索Json.NET ...

  5. Newtonsoft.Json(Json.net)的基本用法

    Newtonsoft.Json(Json.net)的基本用法 其它资料 JSON详解 添加引用: 使用NuGet,命令:install-package Newtonsoft.Json 实体类: pub ...

  6. Newtonsoft.Json 的基本用法

    Ø  前言 说起 C# 对 JSON 的操作(序列化与反序列化),大家都会想到 JavaScriptSerializer.DataContractJsonSerializer 与 Newtonsoft ...

  7. Newtonsoft.Json[C#]

    C# Newtonsoft.Json JsonSerializerSettings配置序列化操作 https://blog.csdn.net/u011127019/article/details/72 ...

  8. Newtonsoft.Json高级篇:TypeNameHandling设置

    原文:Newtonsoft.Json高级篇:TypeNameHandling设置 此示例使用TypeNameHandling 设置在序列化JSON和读取类型信息时包含类型信息,以便在反序列化JSON时 ...

  9. 关于Newtonsoft.Json引用报错

    自己运行的vs版本是2012,然后同事用了2017的,我把代码发给他后运行发现报以下错误: {未能加载文件或程序集"Newtonsoft.Json, Version=4.5.0.0, Cul ...

  10. C# 中Newtonsoft.Json的安装和使用

    官网参考:http://json.codeplex.com/ 在程序包管理控制台,键入NuGet命令  install-package Newtonsoft.Json  安装Newtonsoft.Js ...

随机推荐

  1. Application路径

    根目录:StreamingAssets文件夹 #if UNITY_EDITOR string filepath = Application.dataPath +"/StreamingAsse ...

  2. unity3d中的http通信

    转载 http://blog.csdn.net/mfc11/article/details/8188785的博客,如果侵权,请留言我及时删除! 前言 Unity3d 是一个跨平台的引擎,在移动互联网浪 ...

  3. Robot Motion

    Description A robot has been programmed to follow the instructions in its path. Instructions for the ...

  4. myclips常用快捷键

    -------------------------------------MyEclipse 快捷键1(CTRL)-------------------------------------Ctrl+1 ...

  5. YII 主题

    heming是一个在Web应用程序里定制网页外观的系统方式.通过采用一个新的主题,网页应用程序的整体外观可以立即和戏剧性的改变. 在Yii,每个主题由一个目录代表,包含view文件,layout文件和 ...

  6. php pack、unpack、ord 函数使用方法

    string pack ( string $format [, mixed $args [, mixed $... ]] ) Pack given arguments into a binary st ...

  7. VIM大作战之C++简易集成编译环境(Windows篇)

    一切都要从这篇文章说起 Vim 实在是精致独特得有点像个林妹妹.但谁要是希望家里也有个林妹妹,光把自家丫头照着绣像打扮打扮是不行的,必须从零开始养成一个.而且就算真能养出来个“天上掉下来”一般的可人儿 ...

  8. shell command使用技巧

    1窗口可以merge 2.可以通过 control+t打开窗口

  9. asp.net 由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值

    看MS给的解决方案:(http://support.microsoft.com/kb/312629/ ) 症状:如果使用 Response.End.Response.Redirect 或 Server ...

  10. 【模拟】FOJ 2244 Daxia want to buy house

    题目链接: http://acm.fzu.edu.cn/problem.php?pid=2244 题目大意: 每月还款额=贷款本金×[月利率×(1+月利率)^还款月数]÷[(1+月利率)^还款月数-1 ...