首页后端开发ASP.NETASP.NET MVC 遇到JSON循环调用的问题应该怎么解决?

ASP.NET MVC 遇到JSON循环调用的问题应该怎么解决?

时间2024-01-30 11:31:03发布访客分类ASP.NET浏览635
导读:收集整理的这篇文章主要介绍了ASP.NET MVC 遇到JSON循环调用的问题应该怎么解决?,觉得挺不错的,现在分享给大家,也给大家做个参考。1..Net开源Json序列化工具Newtonsoft.Json中提供了解决序列化的循环引用问题:...
收集整理的这篇文章主要介绍了ASP.NET MVC 遇到JSON循环调用的问题应该怎么解决?,觉得挺不错的,现在分享给大家,也给大家做个参考。1..Net开源Json序列化工具Newtonsoft.Json中提供了解决序列化的循环引用问题:

方式1:指定Json序列化配置为 ReferenceLoopHandling.Ignore

方式2:指定 JsonIgnore忽略 引用对象

实例1,解决MVC的Json序列化引用方法:

step1:在项目上添加引用 Newtonsoft.Json程序包,命令:Insert-Package Newtonsoft.Json

step2:在项目中添加一个类,继承JsonResult,代码如下:

@H_304_15@
/// 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(){
        Liststudent>
     list = _Context.students.Where(q =>
     q.sno == "103").ToList();
    //方法1return Json(list);
//方法2//return new JsonNetResult() {
//    Data=list//}
    ;
}
    

获取的结果,说明,这种方式指定忽略循环引用,是在指定循环级数后忽略,返回的json数据中还是有部分循环的数据

解决EF Json序列化循环引用方法2,在指定的关联对象上,添加JsonIgnore 方法注释

[JsonIgnore]public virtual ICollectionscore>
 scores {
     get;
     set;
 }
    

返回结果中,没有关联表数据


文章转载自:

以上就是ASP.NET MVC 遇到JSON循环调用的问题应该怎么解决?的详细内容,更多请关注其它相关文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

ASP.NETjavascript用的解决

若转载请注明出处: ASP.NET MVC 遇到JSON循环调用的问题应该怎么解决?
本文地址: https://pptw.com/jishu/592463.html
一文了解Node.js中的多进程模型 C# 中十进制与二进制、十六进制、八进制转换详解

游客 回复需填写必要信息