講完了分頁(yè)功能,這一節(jié)我們先不急著實(shí)現(xiàn)新的功能。來(lái)簡(jiǎn)要介紹下Abp中Json的用法。為什么要在這一節(jié)講呢?當(dāng)然是做鋪墊啊,后面的系列文章會(huì)經(jīng)常和Json這個(gè)東西打交道。
一、Json是干什么的
JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式。 易于人閱讀和編寫(xiě)。同時(shí)也易于機(jī)器解析和生成。JSON采用完全獨(dú)立于語(yǔ)言的文本格式,但是也使用了類(lèi)似于C語(yǔ)言家族的習(xí)慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 這些特性使JSON成為理想的數(shù)據(jù)交換語(yǔ)言。
Json一般用于表示:
名稱(chēng)/值對(duì):
{"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"}數(shù)組:
{ "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中默認(rèn)提供了JsonResult來(lái)處理需要返回Json格式數(shù)據(jù)的情況。
一般我們可以這樣使用:
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基類(lèi)中提供的虛方法。
返回的json結(jié)果格式化后為:
[ { "Title": "Ghostbusters", "Genre": "Comedy", "ReleaseDate": "http://Date(1483200000000)//" }, { "Title": "Gone with Wind", "Genre": "Drama", "ReleaseDate": "http://Date(1483372800000)//" }, { "Title": "Star Wars", "Genre": "Science Fiction", "ReleaseDate": "http://Date(1485100800000)//" }]仔細(xì)觀察返回的json結(jié)果,有以下幾點(diǎn)不足:
返回的字段大小寫(xiě)與代碼中一致。這就要求我們?cè)谇岸酥幸惨c代碼中用一致的大小寫(xiě)進(jìn)行取值(item.Title,item.Genre,item.ReleaseDate)。
不包含成功失敗信息:如果我們要判斷請(qǐng)求是否成功,我們要手動(dòng)通過(guò)獲取json數(shù)據(jù)包的length獲取。
返回的日期未格式化,在前端還需自行格式化輸出。
三、Abp中對(duì)Json的封裝
所以Abp封裝了AbpJsonResult繼承于JsonResult,其中主要添加了兩個(gè)屬性:
CamelCase:大小駝峰(默認(rèn)為true,即小駝峰格式)
Indented :是否縮進(jìn)(默認(rèn)為false,即未格式化)
并在AbpController中重載了Controller的Json()方法,強(qiáng)制所有返回的Json格式數(shù)據(jù)為AbpJsonResult類(lèi)型,并提供了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 };}
新聞熱點(diǎn)
疑難解答
圖片精選