Introduction to the usage of Json formatting in the ABP introductory series

高洛峰
Release: 2017-03-19 10:27:43
Original
2434 people have browsed it

,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"}
Copy after login

Array:

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

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);
}
Copy after login

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)\/"
 }
]
Copy after login

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 name="data">Data.</param>
/// <param name="contentType">Content type.</param>
/// <param name="contentEncoding">Content encoding.</param>
/// <param name="behavior">Behavior.</param>
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
 };
}
Copy after login

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
}
Copy after login

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)
Copy after login

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);
 }
 }
Copy after login

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
}
Copy after login

当不需要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 cref="DontWrapResultAttribute"/> 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 cref="WrapResultAttribute"/> class.
  /// </summary>
  /// <param name="wrapOnSuccess">Wrap result on success.</param>
  /// <param name="wrapOnError">Wrap result on error.</param>
  public WrapResultAttribute(bool wrapOnSuccess = true, bool wrapOnError = true)
  {
   WrapOnSuccess = wrapOnSuccess;
   WrapOnError = wrapOnError;
   LogError = true;
  }
 }
}
Copy after login

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

四、Json日期格式化

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

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

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

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!