,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:
1 |
|
Array:
1 2 3 4 5 6 |
|
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:
1 2 3 4 5 6 7 8 |
|
where Json() is the virtual method provided in the Controller base class.
The returned json result is formatted as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
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().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
In ABP, use Controller to inherit from AbpController, use return Json() directly, and format the returned Json result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
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.
1 2 3 |
|
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
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格式化输出以下结果:
1 2 3 4 5 6 7 8 9 10 |
|
当不需要abp对json进行封装包裹怎么办?
简单。只需要在方法上标记[DontWrapResult]特性即可。这个特性其实是一个快捷方式用来告诉ABP不要用AbpJsonResult包裹我,看源码就明白了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
在AbpResultFilter和AbpExceptionFilter过滤器中会根据WrapResultAttribute、DontWrapResultAttribute特性进行相应的过滤。
四、Json日期格式化
第一种办法:前端JS转换:
1 2 3 4 5 6 |
|
第二种办法:在Abp的WepApiModule(模块)中指定JsonFormatter的时间序列化时间格式。
1 |
|
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!