有沒有可能讓jQuery正確解釋JSON中的Date對象?

伺服器端WebAPI使用http://Json.NET返回josn,客戶端使用jQuery的ajax方法調用,http://Json.NET默認使用IsoDateTimeConverter格式化Date對象,實際應用中發現返回的是字元串格式的對象,如果使用http://Json.NET提供的另外一個JavaScriptDateTimeConverter返回Date對象,但jQuery解釋時會拋異常。

請問各位究竟要返回怎樣的格式,jQuery的ajax方法才能正確解釋json裡面的日期類型對象為Date類型對象?或者有沒有什麼方法可以替換掉jQuery默認的解釋程序?


可以看下 JSON 規範,對於 JSON 的定義裡面,沒有設計 Date 這種類型的數據,只有下面幾種

所以不只是 jQuery 所有解析 JSON 的都無法解析 Date object。不過你可以自己設計一個規範包裝一下偽造類似的功能。比如在創建 JSON 的時候,自己設計一個規則來用字元串描述某個數據為 Date 類型的,比如 "time|2017-01-01" 這樣的字元串。然後在伺服器端收到 JSON 的地方,統一做一層遍歷解析 string 的 value 是不是 time 開頭的,如果是那麼將這個值轉成伺服器端的 Date object 再交給後續的操作。不過這種方案基本沒用。


伺服器應該返回純粹的JSON數據,對於日期型欄位,返回字元串或者時間戳都可以,比如:

dateBegin: "2000-01-01"

or

dateBegin: 946684800000

使用$.ajax得到數據後,怎麼讓這些日期欄位的類型變成Date的實例呢?我想樓主需要的是:

jQuery API Documentation jQuery.ajaxSetup( options )

jQuery API Documentation dataFilter

dataFilter

Type: Function( String data, String type ) =&> Anything

A function to be used to handle the raw response data of XMLHttpRequest. This is a pre-filtering function to sanitize the response. You should return the sanitized data. The function accepts two arguments: The raw data returned from the server and the "dataType" parameter.

例:

https://github.com/GreenTeaEc/GreenTea1412/blob/504609021c908cc198e10f5d1665815c49da8196/framework/images/webapp/images/util.js


請看JSON.parse的第二個參數的用法 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse


不能,json本身就沒有Date


當標準與實際需求衝突時,應該考慮支持可選配置的擴展標準,或者採用適合需求的自定義標準。

比如 fastCSharp 的 JSON 組件支持 new Date(n) 這種格式;對於 WebView 輸出為了適合自動化處理(同時對數組進行了壓縮處理)甚至採用了自定義格式,比如 提交記錄 - 51Nod 這個頁面的數據輸出樣例如下

fastCSharp.Pub.AjaxCallBack({currentUser:diantou.user.Get({Balance:0,CanSetEmail:0,ContestCount:0,ContestSubmitCount:0,Email:"xxx@gmail.com",FocusProblemCount:0,Icon:new diantou.image({Value:"upload/000fb8eb/08cf2d7a784b9c12000003d9.gif"}),Id:2,IsWeixin:0,MentionNewCount:0,MessageNewUserCount:0,Mobile:0x30BA9B92F,Name:"肖進",NodPointCount:134,OnlineJudgeSubmitCount:41,Permission:"Admin",Remark:"C#代碼生成器開源項目fastCSharp的發起者,喜歡常用的簡單演算法。",Sex:"Unknown",SimulationUserId:0}),groupMenu:{Groups:["@.diantou.problemGroup,,Id,Name,Remote[ProblemCount]",[-1,"全部問題",[593]],[1,"基礎題",[42]],[2,"1級演算法題",[20]],[3,"2級演算法題",[29]],[4,"3級演算法題",[41]],[5,"4級演算法題",[80]],[6,"5級演算法題",[113]],[7,"6級演算法題",[95]],[8,"7級演算法題",[84]],[9,"8級演算法題",[59]],[10,"9級演算法題",[24]],[11,"難題",[6]]].FormatView(),IsTopic:0,Topics:["ProblemCount,Topic[@.diantou.topic,,Id,Name]",[593,[0,"全部問題"]],[96,[0x5F5E135,"CodeForces"]],[91,[0x28618,"數論"]],[57,[0x2A38C,"動態規劃"]],[53,[0x5F5E12F,"TopCoder"]],[52,[0x2D304,"組合數學"]],[38,[0x27684,"數據結構"]],[31,[0x5F5E14D,"HackerRank"]],[22,[0x5F5E14F,"Codility"]],[22,[0x5D9E7,"貪心演算法"]],[14,[0x5F5E108,"Timus Online Judge"]],[13,[0x5F5E136,"線段樹"]],[13,[0x32B8D,"二分法"]],[13,[0x289D0,"博弈論"]],[12,[0x25BD0,"斐波那契數列"]],[12,[0x5F5E102,"Project Euler"]],[11,[0x3370C,"圖論"]],[11,[0xA2D46,"構造"]],[10,[0x2FB74,"排序"]],[10,[0x8C218,"容斥原理"]],[10,[0x5F5E142,"階乘"]],[9,[0x27065,"快速傅里葉變換"]],[9,[0x2F3A6,"字元串"]],[9,[0x2DB73,"最大公約數"]],[8,[0x5F5E12A,"大數運算"]],[8,[0x1A0747,"矩陣乘法"]],[8,[0x5F5E157,"快速冪"]],[8,[0x5F5E15C,"狀態壓縮DP"]],[8,[0x5F5E166,"數位DP"]],[8,[0x28CE1,"Hash"]],[8,[0x5F5E179,"單調棧"]],[7,[0x6CAA3,"位運算"]],[7,[0x5F5E159,"樹狀數組"]],[6,[0x289E4,"隨機過程"]],[6,[0x278B4,"質數"]],[6,[0x12FB4F,"組合數"]],[6,[0x5F5E12E,"單調隊列"]],[5,[0x5F5E155,"Spoj"]],[4,[0x27E07,"計算幾何"]],[4,[0x5F5E167,"樹DP"]],[4,[0x65F5B,"最小公倍數"]],[4,[0x5F5E12C,"並查集"]],[4,[0x449F4,"中位數"]],[4,[0x2AE90,"線性代數"]],[3,[0x5F5E104,"生成函數"]],[3,[0x5F5E121,"逆序數"]],[3,[0x5F5E10D,"分治"]],[3,[0x5F5E184,"尺取法"]],[3,[0x5F5E10F,"POJ"]],[3,[0x5F5E109,"二分圖"]],[3,[0x5BB06,"最小生成樹"]],[3,[0x6750A,"最短路徑"]],[3,[0x5F5E15A,"伯努利數"]],[3,[0xA656D,"約瑟夫環"]],[3,[0xBB02C,"分解質因數"]],[3,[0x3A520,"枚舉"]],[3,[0x74049,"模擬"]],[2,[0x5F5E16E,"深度優先搜索"]],[2,[0x5F5E130,"RMQ"]],[2,[0x5F5E13E,"漢密爾頓迴路"]],[2,[0x5F5E164,"插頭Dp"]],[2,[0x10DC2A,"生成樹"]],[2,[0x4399F,"網路流"]],[2,[0x5BB29,"寬度優先搜索"]],[2,[0x87AD7,"鏈表"]],[2,[0xBC9C3,"背包問題"]],[2,[0x277D2,"矩陣"]],[2,[0x272FB,"搜索"]],[2,[0x2D486,"概率論"]],[1,[0x27F1D,"資訊理論"]],[1,[0x5F5E160,"後綴數組"]],[1,[0x29E3F,"集合論"]],[1,[0x5F5E12B,"字典樹Trie"]],[1,[0x5F5E169,"康托展開"]],[1,[0x5F5E187,"分塊"]],[1,[0x5F5E186,"前綴和"]],[1,[0x26F5E,"演算法"]],[1,[0x5F5E16F,"弱連通"]],[1,[0x273A8,"抽屜原理"]],[1,[0x5F5E15D,"分數規劃"]],[1,[0x2C200,"棧"]],[1,[0x5F5E15B,"離線處理"]],[1,[0x14316C,"迪傑斯特拉演算法"]],[1,[0x159723,"函數的單調性"]],[1,[0x5F5E103,"歐幾里得擴展"]],[1,[0x5F5E117,"Hu-Tucker演算法"]],[1,[0x9BE6C,"kmp演算法"]],[1,[0x5F5E13F,"ZOJ"]],[1,[0x98D4A,"牛頓迭代法"]],[1,[0x3C785,"哈夫曼樹"]],[1,[0x5F5E158,"質點分解"]],[1,[0x5F5E154,"CodeChef"]],[1,[0x66A20,"並行"]],[1,[0x5F5E188,"後綴樹"]]].FormatView(),ViewOnly:0},judges:["@.diantou.userJudge,,Id,IsCompileError,Judge[@.diantou.judge,,ErrorCount,Id,IsFinished,IsPublic,JudgeValue,Language,MemoryUse,Remote[Problem[@.diantou.problem,,BasePoint,Id,Title]User[@.diantou.user,,Icon[@diantou.image,,Value]Id,Name,Sex]]SubmitTime,TestCount,TimeUse]",[0x2CFBF,0,[0x19,0x2CFBF,1,0,"RunTimeError","Python3",0x7AD000,[[2560,1167,"計算遞推"],[[""],0x560D,"cbg","Unknown"]],new Date(0x1593A1182F7),0x19,78]],[0x2CFBE,0,[0x19,0x2CFBE,1,1,"WrongAnswer","CPlus11",0xD89000,[[160,1604,"對稱的方格顏色"],[[""],0x4533,"wtw","Unknown"]],new Date(0x1593A0E7966),0x1E,109]],[0x2CFBD,0,[0,0x2CFBD,1,1,"Accepted","CPlus",0x1E0000,[[5,1182,"完美字元串"],[["upload/000FC195/08D429C4C5835C9B0000000000000007.png"],0x545C,"王彧之","Unknown"]],new Date(0x1593A0CFDA5),0x14,15]],[0x2CFBC,0,[0x1B,0x2CFBC,1,1,"WrongAnswer","CPlus11",0xD89000,[[160,1604,"對稱的方格顏色"],[[""],0x4533,"wtw","Unknown"]],new Date(0x1593A0C49F9),0x1E,125]],[0x2CFBB,1,[0,0x2CFBB,1,0,"CompileError","C",0,[[2560,1167,"計算遞推"],[[""],0x560D,"cbg","Unknown"]],new Date(0x1593A0C1CDB),0,0]],[0x2CFBA,0,[0,0x2CFBA,1,1,"Accepted","Javascript",0x1096000,[[0,1000,"A + B"],[[""],0x28EF,"hojas","Unknown"]],new Date(0x1593A01978F),0x14,265]],[0x2CFB9,0,[0,0x2CFB9,1,1,"Accepted","CPlus",0x206000,[[0,1058,"N的階乘的長度"],[["upload/000FC158/08D3FBE64F46B042000000000000018A.jpeg"],0x4E6C,"王自強","Unknown"]],new Date(0x1593A004440),0x19,15]],[0x2CFB8,1,[0,0x2CFB8,1,0,"CompileError","C",0,[[2560,1167,"計算遞推"],[[""],0x560D,"cbg","Unknown"]],new Date(0x15939FFFD82),0,0]],[0x2CFB7,0,[0,0x2CFB7,1,1,"Accepted","Javascript",0x1099000,[[0,1000,"A + B"],[[""],0x28EF,"hojas","Unknown"]],new Date(0x15939FE4C69),0x14,265]],[0x2CFB6,0,[0x06,0x2CFB6,1,0,"WrongAnswer","C",0x1AC000,[[40,1186,"質數檢測 V2"],[[""],0x560D,"cbg","Unknown"]],new Date(0x15939FD602C),0x14,437]],[0x2CFB5,0,[0,0x2CFB5,1,1,"Accepted","CPlus",0x5E9000,[[0,1183,"編輯距離"],[["upload/000FC158/08D3FBE64F46B042000000000000018A.jpeg"],0x4E6C,"王自強","Unknown"]],new Date(0x15939FAB20A),0x14,31]],[0x2CFB4,0,[0,0x2CFB4,1,1,"Accepted","CPlus",0x5E9000,[[0,1183,"編輯距離"],[["upload/000FC158/08D3FBE64F46B042000000000000018A.jpeg"],0x4E6C,"王自強","Unknown"]],new Date(0x15939F94AB1),0x14,15]],[0x2CFB3,0,[0x06,0x2CFB3,1,0,"WrongAnswer","C",0x1AC000,[[40,1186,"質數檢測 V2"],[[""],0x560D,"cbg","Unknown"]],new Date(0x15939F910F5),0x14,406]],[0x2CFB2,0,[0,0x2CFB2,1,1,"Accepted","Java",0x1414000,[[0,1242,"斐波那契數列的第N項"],[["upload/000FBF45/08D2CD99793A545A0000000000000021.jpeg"],8255,"江流兒","Unknown"]],new Date(0x15939F806F7),0x19,390]],[0x2CFB1,0,[0x06,0x2CFB1,1,0,"WrongAnswer","C",0x1AC000,[[40,1186,"質數檢測 V2"],[[""],0x560D,"cbg","Unknown"]],new Date(0x15939F745B3),0x14,421]],[0x2CFB0,0,[0,0x2CFB0,1,1,"Accepted","CPlus",0x1A9000,[[5,1283,"最小周長"],[["upload/000FC195/08D429C4C5835C9B0000000000000007.png"],0x545C,"王彧之","Unknown"]],new Date(0x15939F288BB),0x19,15]],[0x2CFAF,0,[0x06,0x2CFAF,1,0,"WrongAnswer","C",0x1A9000,[[40,1186,"質數檢測 V2"],[[""],0x560D,"cbg","Unknown"]],new Date(0x15939F23B7D),0x14,406]],[0x2CFAE,0,[0x06,0x2CFAE,1,0,"WrongAnswer","C",0x1A9000,[[40,1186,"質數檢測 V2"],[[""],0x560D,"cbg","Unknown"]],new Date(0x15939F1AD75),0x14,406]],[0x2CFAD,0,[0x06,0x2CFAD,1,0,"WrongAnswer","C",0x1A9000,[[40,1186,"質數檢測 V2"],[[""],0x560D,"cbg","Unknown"]],new Date(0x15939F10962),0x14,406]],[0x2CFAC,0,[0,0x2CFAC,1,1,"Accepted","CPlus",0x57E000,[[0,1118,"機器人走方格"],[["upload/000FC158/08D3FBE64F46B042000000000000018A.jpeg"],0x4E6C,"王自強","Unknown"]],new Date(0x15939EE427C),0x14,31]]].FormatView(),noticeMessage:null,pageInfo:new diantou.page({Count:0x2CFB6,Page:0,Path:"/onlineJudge/submitList.html?",Size:20}),pubPath:new fastCSharpPath.pub({}),pubView:{ContestCount:30,ProblemCount:593,ViewOnly:0},query:{language:"All"},requestHeader:{},updateVersion:398})


json沒有Date,返回一個數字,然後new Date創建一個Date類型就行了。


一般我們的項目中json中的日期對象就返回字元串比如:

{
date:"19910101 23:00:11"
}

這種類型

當客戶端接收到相關的json後我們用了一個叫Moment.js 中文網的開源類庫對日期進行操作,這個類庫可以非常方便的在頁面中操作時間方面的數據.


根據以下兩個網頁可以輕鬆得出轉換方法 ——

http://www.newtonsoft.com/json

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date


json里沒有什麼日期類型

你的json必須是得正確

任何json解析庫都不會腦殘到不能解析正確的json


推薦閱讀:

ASP.NET 5 和 ASP.NET MVC 6 是什麼關係,是不是要分裂了,是一伙人在弄嗎?
ASP.NET高級教程中為什麼側重的是各種控制項的使用,而不是代碼部分?
C#具體的學習或發展方向有那些?

TAG:jQuery | ASPNET | JSON | ASPNET開發 | ASPNETMVC |