ABP入门系列之Json格式化

2019-05-25 22:38:30丽君

讲完了分页功能,这一节我们先不急着实现新的功能。来简要介绍下Abp中Json的用法。为什么要在这一节讲呢?当然是做铺垫啊,后面的系列文章会经常和Json这个东西打交道。

一、Json是干什么的

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语言。

Json一般用于表示:

名称/值对:

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

数组:

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

二、Asp.net Mvc中的JsonResult

Asp.net mvc中默认提供了JsonResult来处理需要返回Json格式数据的情况。

一般我们可以这样使用:

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);
}

其中Json()是Controller基类中提供的虚方法。

返回的json结果格式化后为:

[
 {
 "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)/"
 }
]

仔细观察返回的json结果,有以下几点不足:

返回的字段大小写与代码中一致。这就要求我们在前端中也要与代码中用一致的大小写进行取值(item.Title,item.Genre,item.ReleaseDate)。

不包含成功失败信息:如果我们要判断请求是否成功,我们要手动通过获取json数据包的length获取。

返回的日期未格式化,在前端还需自行格式化输出。

三、Abp中对Json的封装

所以Abp封装了AbpJsonResult继承于JsonResult,其中主要添加了两个属性:

CamelCase:大小驼峰(默认为true,即小驼峰格式)

Indented :是否缩进(默认为false,即未格式化)

并在AbpController中重载了Controller的Json()方法,强制所有返回的Json格式数据为AbpJsonResult类型,并提供了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
 };
}