mvc 使用Newtonsoft.Json进行序列化json数据

本文介绍了如何在.NET MVC项目中使用Newtonsoft.Json替代系统默认的System.Web.Script.Serialization进行JSON序列化。通过代码示例展示了如何创建自定义的JsonResult,并提供了扩展Controller的方法,使得返回JSON数据更加便捷,同时强调了前端接收的大写格式的JSON数据更符合规范。
摘要由CSDN通过智能技术生成

.net mvc服务器段返回json数据采用的是System.Web.Script.Serialization进行序列化的,但是.net自带的序列化工具没有Newtonsoft.Json第三方好用,我们现在来使Newtonsoft.Json替换用JsonResult,打造属于自己的jsonResult。

具体的代码如下

添加引用

using System;
using System.IO;
using System.Text;
using System.Web.Mvc;
using Aft.Build.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;


public class JsonNetResult : JsonResult
    {
        public JsonNetResult()
        {
            Settings = new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Error
            };
        }
        public JsonNetResult(object data, JsonRequestBehavior behavior = JsonRequestBehavior.AllowGet, string contentType = null, Encoding contentEncoding = null)
        {
            Data = data;
            JsonRequestBehavior = behavior;
            Content
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值