Asp.Net Core中Json序列化处理整理

一、Asp.Net Core中的Json序列化处理使用的是Newtonsoft.Json,更多参考:C# Newtonsoft.Json JsonSerializerSettings配置序列化操作C# Json序列化工具--Newtonsoft.Json简介和使用

1.Newtonsoft.Json仅 依赖.Net Standard所以支持.Net Framework也支持.Net Core

2.更多说明

/*
* 1.在Core Mvc中JsonResult 默认支持Get请求
* 2.使用JQuery的ajax请求,返回json数据自动转换成 object对象
* 3.在 Core Mvc的 后台JsonResult序列化的时候,默认情况下自动 处理 的命名规则,改成了 js的驼峰格式
* 4.在 Core Mvc中json 的序列化发序列化使用的是Newtonsoft.Json库
* 5.默认没有处理循环引用的问题
*/

二、使用实例

Jquery 的ajax get请求

$('#btnOne').click(function () {
//使用ajax get请求json 数据
$.get('@Url.Action("DataOne")', {}, function (data) {
console.info(data);
console.info(data[0].menuName);
});
});

1.默认情况,使用驼峰样式处理字段名Key

public JsonResult DataThree()
{
//Ef Core现在 不支持延迟加载,对于关联表数据都为null
List<Menu> menus = _context.Menu
.ToList();
return Json(menus);
}

Asp.Net Core中Json序列化处理整理

2.设置不使用驼峰格式处理,由后台字段确定大小写,也就是默认格式

public JsonResult DataOne()
{
List<Menu> menus = _context.Menu.ToList();
JsonSerializerSettings settings = new JsonSerializerSettings();
//EF Core中默认为驼峰样式序列化处理key
//settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
//使用默认方式,不更改元数据的key的大小写
settings.ContractResolver = new DefaultContractResolver(); return Json(menus, settings);
}

Asp.Net Core中Json序列化处理整理

3.处理循环引用,加载关联表数据、

public JsonResult DataTwo()
{
List<Menu> menus = _context.Menu
.Include(q => q.Model)
.ToList();
//处理循环引用问题
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MaxDepth = ;
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //设置不处理循环引用
return Json(menus, settings);
}

Asp.Net Core中Json序列化处理整理

三、全局设置,Json序列化配置

IMvcBuilder依赖注入扩展了MvcJsonMvcBuilderExtensions配置

定义如下 :

    //
// 摘要:
// Extensions methods for configuring MVC via an Microsoft.Extensions.DependencyInjection.IMvcBuilder.
public static class MvcJsonMvcBuilderExtensions
{
//
// 摘要:
// Adds configuration of Microsoft.AspNetCore.Mvc.MvcJsonOptions for the application.
//
// 参数:
// builder:
// The Microsoft.Extensions.DependencyInjection.IMvcBuilder.
//
// setupAction:
// The Microsoft.AspNetCore.Mvc.MvcJsonOptions which need to be configured.
public static IMvcBuilder AddJsonOptions(this IMvcBuilder builder, Action<MvcJsonOptions> setupAction);
}

MvcJsonOptions:

    //
// 摘要:
// Provides programmatic configuration for JSON in the MVC framework.
public class MvcJsonOptions
{
public MvcJsonOptions(); //
// 摘要:
// Gets the Newtonsoft.Json.JsonSerializerSettings that are used by this application.
public JsonSerializerSettings SerializerSettings { get; }
}

在Startup文件中修改

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc()
//全局配置Json序列化处理
.AddJsonOptions(options =>
{
//忽略循环引用
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
//不使用驼峰样式的key
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
//设置时间格式
options.SerializerSettings.DateFormatString = "yyyy-MM-dd";
}
);
}

更多:

.NetCore中EFCore的使用整理(二)-关联表查询

Asp.Net Core MVC控制器和视图之间传值

.NetCore中EFCore的使用整理

上一篇:Ceph与OpenStack的Nova相结合


下一篇:CentOS 下如何查看并清理系统内存空间