在.NET Web API設置響應輸出Json數(shù)據(jù)格式常用的兩種方式詳解
前言
在ASP.NET Core Web API中設置響應輸出Json數(shù)據(jù)格式常用以下兩種方式:可以通過添加System.Text.Json
或Newtonsoft.Json
JSON序列化和反序列化庫在應用程序中全局設置接口響應的Json數(shù)據(jù)格式。
注意:本文示例使用的是新的Minimal API模式。
JSON序列化和反序列化庫
System.Text.Json
System.Text.Json是 .NET Core 3.0 及以上版本中內(nèi)置的 JSON 序列化和反序列化庫。
Newtonsoft.Json
Newtonsoft.Json是一個功能強大且靈活的.NET JSON序列化和反序列化庫,用于在.NET應用程序中處理JSON數(shù)據(jù)。
需求設置統(tǒng)一格式
- 修改屬性名稱的序列化方式,在.Net Core中默認使用小駝峰序列化Json屬性參數(shù),前端想要使用與后端模型本身命名格式輸出(如:UserName)。
- 日期類型默認格式化處理,設置為:yyyy-MM-dd HH:mm:ss。
未配置之前的API輸出Json數(shù)據(jù)
UserInfoModel
public class UserInfoModel { public DateTime DateTime { get; set; } public int NumberIndex { get; set; } public string UserName { get; set; } }
UserInfoController
[ApiController] [Route("[controller]")] public class UserInfoController : ControllerBase { private static readonly string[] NameList = new[] { "追逐時光者", "小明同學", "DotNetGuide", "小藝同學", "Edwin" }; [HttpGet(Name = "GetUserInfo")] public IEnumerable<UserInfoModel> Get() { return Enumerable.Range(1, 5).Select(index => new UserInfoModel { DateTime = DateTime.Now.AddDays(index), NumberIndex = Random.Shared.Next(-20, 55), UserName = NameList[Random.Shared.Next(NameList.Length)] }).ToArray(); } }
輸出Json數(shù)據(jù)
System.Text.Json程序全局配置
添加自定義時間輸出格式類(DateTimeJsonConverter)
public class DateTimeJsonConverter : JsonConverter<DateTime> { public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { return DateTime.Parse(reader.GetString()); } public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) { writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss")); } }
在Program.cs中全局配置
builder.Services.AddControllers().AddJsonOptions(options => { //命名規(guī)則,該值指定用于將對象上的屬性名稱轉換為另一種格式(例如駝峰大小寫)或為空以保持屬性名稱不變的策略[前端想要使用與后端模型本身命名格式輸出]。 options.JsonSerializerOptions.PropertyNamingPolicy = null; //自定義輸出的時間格式 options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter()); });
配置后輸出的Json數(shù)據(jù)
Newtonsoft.Json程序全局配置
說明
在.NET 3.0及其以上的版本使用Newtonsoft.Json需要通過安裝 Microsoft.AspNetCore.Mvc.NewtonsoftJson
包來進行配置(注意假如提示該包安裝失敗可以嘗試安裝其他版本的包)。
在Program.cs中全局配置
builder.Services.AddControllers().AddNewtonsoftJson(options => { //修改屬性名稱的序列化方式[前端想要使用與后端模型本身命名格式輸出] options.SerializerSettings.ContractResolver = null; //方式1:日期類型默認格式化處理 options.SerializerSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }); //方式2:日期類型默認格式化處理 //options.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat; //options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; });
配置后輸出的Json數(shù)據(jù)
DotNetGuide技術社區(qū)交流群
- DotNetGuide技術社區(qū)是一個面向.NET開發(fā)者的開源技術社區(qū),旨在為開發(fā)者們提供全面的C#/.NET/.NET Core相關學習資料、技術分享和咨詢、項目框架推薦、求職和招聘資訊、以及解決問題的平臺。
- 在DotNetGuide技術社區(qū)中,開發(fā)者們可以分享自己的技術文章、項目經(jīng)驗、學習心得、遇到的疑難技術問題以及解決方案,并且還有機會結識志同道合的開發(fā)者。
- 我們致力于構建一個積極向上、和諧友善的.NET技術交流平臺。無論您是初學者還是有豐富經(jīng)驗的開發(fā)者,我們都希望能為您提供更多的價值和成長機會。
參考文章
到此這篇關于在.NET Web API設置響應輸出Json數(shù)據(jù)格式常用的兩種方式的文章就介紹到這了,更多相關.NET Web API響應輸出Json數(shù)據(jù)格式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
.NET?6新特性試用之DateOnly和TimeOnly類型
這篇文章主要介紹了.NET?6新特性試用之DateOnly和TimeOnly類型,主要介紹DateOnly和TimeOnly類型使用過程及存在的一些過程,需要的小伙伴可以參考一下2022-03-03詳解高效而穩(wěn)定的企業(yè)級.NET Office 組件Spire(.NET組件介紹之二)
這篇文章主要介紹了詳解高效而穩(wěn)定的企業(yè)級.NET Office 組件Spire(.NET組件介紹之二),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。2016-12-12ASP.NET Core MVC學習之視圖組件(View Component)
這篇文章主要給大家介紹了關于ASP.NET Core MVC學習之視圖組件(View Component)的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用ASP.NET Core MVC具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-08-08使CheckBoxList的Attributes屬性生效(修改微軟的一個bug)
使CheckBoxList的Attributes屬性生效(修改微軟的一個bug)...2007-08-08