search
HomeBackend DevelopmentC#.Net TutorialIntroduction to the usage of Json formatting in the ABP introductory series

,JSON(JavaScript Object Notation) is a lightweight data exchange format. This article focuses on introducing Json formatting in the ABP introductory series. Friends who need it can refer to

. After talking about the paging function, we are not in a hurry to implement new functions in this section. Let’s briefly introduce the usage of Json in Abp. Why do we talk about it in this section? Of course, this is to pave the way. The following series of articles will often deal with Json.

1. What does Json do?

JSON (Javascript Object Notation) is a lightweight data exchange format. Easy for humans to read and write. It is also easy for machines to parse and generate. JSON uses a completely language-independent text format, but also uses conventions similar to the C language family (including C, C++, C#, Java, Javascript, Perl, Python, etc.). These properties make JSON an ideal data exchange language.

Json is generally used to represent:

Name/value pair:

{"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"}

Array:

{ "people":[
  {"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"},
  {"firstName":"Jason","lastName":"Hunter","email":"bbbb"},
  {"firstName":"Elliotte","lastName":"Harold","email":"cccc"}
 ]
}

II, JsonResult in Asp.net Mvc

Asp.net mvc provides JsonResult by default to handle situations where Json format data needs to be returned.

Generally we can use it like this:

public ActionResult Movies()
{
 var movies = new List<object>();
 movies.Add(new { Title = "Ghostbusters", Genre = "Comedy", ReleaseDate = new DateTime(2017,1,1) });
 movies.Add(new { Title = "Gone with Wind", Genre = "Drama", ReleaseDate = new DateTime(2017, 1, 3) });
 movies.Add(new { Title = "Star Wars", Genre = "Science Fiction", ReleaseDate = new DateTime(2017, 1, 23) });
 return Json(movies, JsonRequestBehavior.AllowGet);
}</object>

where Json() is the virtual method provided in the Controller base class.

The returned json result is formatted as:

[
 {
 "Title": "Ghostbusters",
 "Genre": "Comedy",
 "ReleaseDate": "\/Date(1483200000000)\/"
 },
 {
 "Title": "Gone with Wind",
 "Genre": "Drama",
 "ReleaseDate": "\/Date(1483372800000)\/"
 },
 {
 "Title": "Star Wars",
 "Genre": "Science Fiction",
 "ReleaseDate": "\/Date(1485100800000)\/"
 }
]

Carefully observe the returned json result, there are the following shortcomings:

The returned field case is consistent with the code . This requires us to use the same case in the front end as in the code (item.Title, item.Genre, item.ReleaseDate).

Does not contain success or failure information: If we want to determine whether the request is successful, we have to manually obtain the length of the json data packet.

The returned date is not formatted and needs to be formatted and output on the front end.

3. Encapsulation of Json in Abp

So Abp encapsulates AbpJsonResultInherits from JsonResult, which mainly adds Two attributes:

CamelCase: size of camel case (default is true, that is, small camel case format)

Indented: whether to indent (default is false, that is, not formatted)

And in AbpController Overloaded the Json() method of the Controller, forcing all returned Json format data to be of the AbpJsonResult type, and providing a virtual method of AbpJson().

/// <summary>
/// Json the specified data, contentType, contentEncoding and behavior.
/// </summary>
/// <param>Data.
/// <param>Content type.
/// <param>Content encoding.
/// <param>Behavior.
protected override JsonResult Json(object data, string contentType, 
 Encoding contentEncoding, JsonRequestBehavior behavior)
{
 if (_wrapResultAttribute != null && !_wrapResultAttribute.WrapOnSuccess)
 {
  return base.Json(data, contentType, contentEncoding, behavior);
 }
 return AbpJson(data, contentType, contentEncoding, behavior);
}
protected virtual AbpJsonResult AbpJson(
 object data,
 string contentType = null,
 Encoding contentEncoding = null,
 JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet,
 bool wrapResult = true,
 bool camelCase = true,
 bool indented = false)
{
 if (wrapResult)
 {
  if (data == null)
  {
   data = new AjaxResponse();
  }
  else if (!(data is AjaxResponseBase))
  {
   data = new AjaxResponse(data);
  }
 }
 return new AbpJsonResult
 {
  Data = data,
  ContentType = contentType,
  ContentEncoding = contentEncoding,
  JsonRequestBehavior = behavior,
  CamelCase = camelCase,
  Indented = indented
 };
}

In ABP, use Controller to inherit from AbpController, use return Json() directly, and format the returned Json result:

{
 "result": [
 {
  "title": "Ghostbusters",
  "genre": "Comedy",
  "releaseDate": "2017-01-01T00:00:00"
 },
 {
  "title": "Gone with Wind",
  "genre": "Drama",
  "releaseDate": "2017-01-03T00:00:00"
 },
 {
  "title": "Star Wars",
  "genre": "Science Fiction",
  "releaseDate": "2017-01-23T00:00:00"
 }
 ],
 "targetUrl": null,
 "success": true,
 "error": null,
 "unAuthorizedRequest": false,
 "abp": true
}

The result is the data returned specified in the code. Several other key-value pairs are encapsulated by ABP, including authentication, success, error information, and target Url. Are these parameters very sweet?

You can also specify parameters for json formatted output by calling return AbpJson().

If you look carefully, you will find that the date format is still weird. 2017-01-23T00:00:00, one more T. Looking at the AbpJsonReult source code, we found that JsonConvert.SerializeObject(obj, settings); in the Newtonsoft.Json Serialization component is called for serialization.

Check the introduction of Newtonsoft.Json official website. For date formatting output, you need to specify the DateTimeFormat of IsoDateTimeConverter.

IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
   timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
JsonConvert.SerializeObject(dt, Formatting.Indented, timeFormat)

So how do we specify this DateTimeFormat in our Abp?

The AbpDateTimeConverter class provided in ABP inherits from IsoDateTimeConverter.

But look at the Json serialization extension class integrated in ABP:

public static class JsonExtensions
 {
 /// <summary>Converts given object to JSON string.</summary>
 /// <returns></returns>
 public static string ToJsonString(this object obj, bool camelCase = false, bool indented = false)
 {
  JsonSerializerSettings settings = new JsonSerializerSettings();
  if (camelCase)
  settings.ContractResolver = (IContractResolver) new CamelCasePropertyNamesContractResolver();
  if (indented)
  settings.Formatting = Formatting.Indented;
  settings.Converters.Insert(0, (JsonConverter) new AbpDateTimeConverter());
  return JsonConvert.SerializeObject(obj, settings);
 }
 }

Obviously DateTimeFormat is not specified, so we can only do it ourselves. For specific codes, please refer to 4 ways to solve json date format problems The fourth method of the method.

当有异常发生时,Abp返回的Json格式化输出以下结果:

{
 "targetUrl": null,
 "result": null,
 "success": false,
 "error": {
 "message": "An internal error occured during your request!",
 "details": "..."
 },
 "unAuthorizedRequest": false
}

当不需要abp对json进行封装包裹怎么办?

简单。只需要在方法上标记[DontWrapResult]特性即可。这个特性其实是一个快捷方式用来告诉ABP不要用AbpJsonResult包裹我,看源码就明白了:

namespace Abp.Web.Models
{
 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method)]
 public class DontWrapResultAttribute : WrapResultAttribute
 {
  /// <summary>
  /// Initializes a new instance of the <see></see> class.
  /// </summary>
  public DontWrapResultAttribute()
   : base(false, false)
  {
  }
 }
 /// <summary>
 /// Used to determine how ABP should wrap response on the web layer.
 /// </summary>
 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method)]
 public class WrapResultAttribute : Attribute
 {
  /// <summary>
  /// Wrap result on success.
  /// </summary>
  public bool WrapOnSuccess { get; set; }
  /// <summary>
  /// Wrap result on error.
  /// </summary>
  public bool WrapOnError { get; set; }
  /// <summary>
  /// Log errors.
  /// Default: true.
  /// </summary>
  public bool LogError { get; set; }
  /// <summary>
  /// Initializes a new instance of the <see></see> class.
  /// </summary>
  /// <param>Wrap result on success.
  /// <param>Wrap result on error.
  public WrapResultAttribute(bool wrapOnSuccess = true, bool wrapOnError = true)
  {
   WrapOnSuccess = wrapOnSuccess;
   WrapOnError = wrapOnError;
   LogError = true;
  }
 }
}

在AbpResultFilter和AbpExceptionFilter过滤器中会根据WrapResultAttribute、DontWrapResultAttribute特性进行相应的过滤。

四、Json日期格式化

第一种办法:前端JS转换:

 //格式化显示json日期格式
 function showDate(jsonDate) {
  var date = new Date(jsonDate);
  var formatDate = date.toDateString();
  return formatDate;
 }

第二种办法:在Abp的WepApiModule(模块)中指定JsonFormatter的时间序列化时间格式。

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.DateFormatString ="yyyy-MM-dd HH:mm:ss";

PS:这种方法仅对WebApi有效。

总结

本节主要讲解了以下几个问题:

Asp.net中JsonResult的实现。

ABP对JsonResult的再封装,支持指定大小驼峰及是否缩进进行Json格式化。

如何对DateTime类型对象进行格式化输出。

Web层通过拓展AbpJsonResult,指定时间格式。

前端,通过将Json日期转换为js的Date类型,再格式化输出。

WebApi,通过在Moduel中指定DateFormatString。

The above is the detailed content of Introduction to the usage of Json formatting in the ABP introductory series. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C# .NET Framework vs. .NET Core/5/6: What's the Difference?C# .NET Framework vs. .NET Core/5/6: What's the Difference?May 07, 2025 am 12:06 AM

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The Community of C# .NET Developers: Resources and SupportThe Community of C# .NET Developers: Resources and SupportMay 06, 2025 am 12:11 AM

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The C# .NET Advantage: Features, Benefits, and Use CasesThe C# .NET Advantage: Features, Benefits, and Use CasesMay 05, 2025 am 12:01 AM

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

Is C# Always Associated with .NET? Exploring AlternativesIs C# Always Associated with .NET? Exploring AlternativesMay 04, 2025 am 12:06 AM

C# is not always tied to .NET. 1) C# can run in the Mono runtime environment and is suitable for Linux and macOS. 2) In the Unity game engine, C# is used for scripting and does not rely on the .NET framework. 3) C# can also be used for embedded system development, such as .NETMicroFramework.

The .NET Ecosystem: C#'s Role and BeyondThe .NET Ecosystem: C#'s Role and BeyondMay 03, 2025 am 12:04 AM

C# plays a core role in the .NET ecosystem and is the preferred language for developers. 1) C# provides efficient and easy-to-use programming methods, combining the advantages of C, C and Java. 2) Execute through .NET runtime (CLR) to ensure efficient cross-platform operation. 3) C# supports basic to advanced usage, such as LINQ and asynchronous programming. 4) Optimization and best practices include using StringBuilder and asynchronous programming to improve performance and maintainability.

C# as a .NET Language: The Foundation of the EcosystemC# as a .NET Language: The Foundation of the EcosystemMay 02, 2025 am 12:01 AM

C# is a programming language released by Microsoft in 2000, aiming to combine the power of C and the simplicity of Java. 1.C# is a type-safe, object-oriented programming language that supports encapsulation, inheritance and polymorphism. 2. The compilation process of C# converts the code into an intermediate language (IL), and then compiles it into machine code execution in the .NET runtime environment (CLR). 3. The basic usage of C# includes variable declarations, control flows and function definitions, while advanced usages cover asynchronous programming, LINQ and delegates, etc. 4. Common errors include type mismatch and null reference exceptions, which can be debugged through debugger, exception handling and logging. 5. Performance optimization suggestions include the use of LINQ, asynchronous programming, and improving code readability.

C# vs. .NET: Clarifying the Key Differences and SimilaritiesC# vs. .NET: Clarifying the Key Differences and SimilaritiesMay 01, 2025 am 12:12 AM

C# is a programming language, while .NET is a software framework. 1.C# is developed by Microsoft and is suitable for multi-platform development. 2..NET provides class libraries and runtime environments, and supports multilingual. The two work together to build modern applications.

Beyond the Hype: Assessing the Current Role of C# .NETBeyond the Hype: Assessing the Current Role of C# .NETApr 30, 2025 am 12:06 AM

C#.NET is a powerful development platform that combines the advantages of the C# language and .NET framework. 1) It is widely used in enterprise applications, web development, game development and mobile application development. 2) C# code is compiled into an intermediate language and is executed by the .NET runtime environment, supporting garbage collection, type safety and LINQ queries. 3) Examples of usage include basic console output and advanced LINQ queries. 4) Common errors such as empty references and type conversion errors can be solved through debuggers and logging. 5) Performance optimization suggestions include asynchronous programming and optimization of LINQ queries. 6) Despite the competition, C#.NET maintains its important position through continuous innovation.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)