mvc 使用Newtonsoft.Json进行序列化json数据
2014-08-14 12:16
435 查看
.net mvc服务器段返回json数据采用的是System.Web.Script.Serialization进行序列化的,但是.net自带的序列化工具没有Newtonsoft.Json第三方好用,我们现在来使Newtonsoft.Json替换用JsonResult,打造属于自己的jsonResult。
具体的代码如下
添加引用
使用方式就跟使用jsonResult方式一样。
但是每次使用new jsonResult这种方式真的很烦人,我希望能在mvc controller action中使用json方法一样返回一个json数据。
接下来我们写两个controller扩展方法,具体代码实现如下:
使用方式:
添加引用:
1.把扩展所在的地方添加引用
或者是
再或者是:
这样,总算是大功告成了。
总结:
1.mvc默认使用Json方法是在controller中使用JsonResult来返回json数据的。
2.前端json数据使用大小写格式更加正规,更加符合命名规范。
具体的代码如下
添加引用
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; ContentEncoding = contentEncoding; ContentType = contentType; } private JsonSerializerSettings _settings; public JsonSerializerSettings Settings { get { _settings = _settings ?? new JsonSerializerSettings(); _settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); return _settings; } private set { _settings = value; } } public override void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) throw new InvalidOperationException("JSON GET is not allowed"); var response = context.HttpContext.Response; response.ContentType = string.IsNullOrEmpty(ContentType) ? "application/json" : ContentType; if (ContentEncoding != null) response.ContentEncoding = ContentEncoding; if (Data == null) return; var scriptSerializer = JsonSerializer.Create(Settings); using (var sw = new StringWriter()) { scriptSerializer.Serialize(sw, Data); response.Write(sw.ToString()); } } }这里我使用Newtonsoft.Json的 ContractResolver是CamelCasePropertyNamesContractResolver(即大小写格式)进行序列化数据。
使用方式就跟使用jsonResult方式一样。
但是每次使用new jsonResult这种方式真的很烦人,我希望能在mvc controller action中使用json方法一样返回一个json数据。
接下来我们写两个controller扩展方法,具体代码实现如下:
<pre name="code" class="csharp">using System; using System.Linq; using System.Web.Mvc; using Aft.Build.MvcWeb.Models; namespace Aft.Build.MvcWeb.Common.Extensions { public static class ControllerExtensions { public static ActionResult JsonNetResult(this Controller controller, Object data) { return new JsonNetResult(data); } public static ActionResult JsonNetResult<T>(this Controller controller, IQueryable<T> query, KendoPagedFilter filter) { var data = DataSourceResult<T>.From(query, filter); return controller.JsonNetResult(data); } } }
使用方式:
添加引用:
1.把扩展所在的地方添加引用
using Aft.Build.MvcWeb.Common.Extensions;
[HttpPost] public ActionResult List(SearchFilter filter) { var query = _countryRepository.AsQueryable(); if (!string.IsNullOrEmpty(filter.Name)) { query = query.Where(e => e.Name.Contains(filter.Name)); } return this.JsonNetResult(query, filter); }
或者是
[HttpPost] public ActionResult Update(ObjectId id, Employee employee) { employee.Id = id; var result = _employeeTask.Save(employee); return this.JsonNetResult(result); }
再或者是:
[HttpPost] public ActionResult Del(ObjectId id) { _countryRepository.Delete(id); return new JsonNetResult(true); }
这样,总算是大功告成了。
总结:
1.mvc默认使用Json方法是在controller中使用JsonResult来返回json数据的。
2.前端json数据使用大小写格式更加正规,更加符合命名规范。
相关文章推荐
- mvc 使用Newtonsoft.Json进行序列化json数据
- mvc使用Newtonsoft.Json反序列化json数据
- mvc “使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错,字符串的长度超过了为 maxJsonLength 属性设置的值” 解决经历
- 使用JSON.Net(Newtonsoft.Json)作为ASP.Net MVC的json序列化和反序列化工具
- 使用Newtonsoft JsonConvert反序列化Json数据到DataTable
- ASP.net MVC使用Jquery-pager和Newtonsoft.Json进行分页
- C#使用NewtonSoft进行Json序列化,设置字段首字母小写方法
- MVC使用Newtonsoft无需实体类,实现JSON数据返回给前端页面使用
- ASP.net MVC使用Jquery-pager和Newtonsoft.Json进行分页
- 使用Newtonsoft.Json进行JSON反序列化操作
- Asp.Net Core中使用Newtonsoft.Json进行序列化处理解决返回值首字母小写
- MVC日期格式化,后台使用Newtonsoft.Json序列化日期,前端使用”f”格式化日期
- c# 使用Newtonsoft.Json 对数据进行打包与解析
- 使用Newtonsoft JsonConvert反序列化Json数据到DataTable
- Spring3 MVC 使用JSON进行前后台数据交互
- asp.net mvc情况下使用jqery ajax的方法进行json数据传递
- .net MVC 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错,字符串的长度超过了为 maxJsonLength 属性设置的值
- 使用Newtonsoft.json 解决 Asp.Net MVC DateTime类型数据Json格式化问题
- 第 10 部分: 使用 JSON 进行数据传输
- 使用 JSON 进行数据传输