动机:.net 默认的时间序列化方式为\/Date(数字 时区)\/,序列化成JSON在前端很难处理,有没有一种好的处理方式呢?

改进:

 JSON = new function(){
this.decode = function(){
var filter, result, self, tmp;
if($$("toString")) {
switch(arguments.length){
case 2:
self = arguments[0];
filter = arguments[1];
break;
case 1:
if($[typeof arguments[0]](arguments[0]) === Function) {
self = this;
filter = arguments[0];
}
else
self = arguments[0];
break;
default:
self = this;
break;
};
if(rc.test(self)){
try{
result = e("(".concat(self, ")"));
if(filter && result !== null && (tmp = $[typeof result](result)) && (tmp === Array || tmp === Object)){
for(self in result)
result[self] = v(self, result) ? filter(self, result[self]) : result[self];
}
}
catch(z){}
}
else {
throw new Error("bad data");
}
};
return result;
};
this.encode = function(){
var self = arguments.length ? arguments[0] : this,
result, tmp;
if(self === null)
result = "null";
else if(self !== undefined && (tmp = $[typeof self](self))) {
switch(tmp){
case Array:
result = [];
for(var i = 0, j = 0, k = self.length; j < k; j++) {
if(self[j] !== undefined && (tmp = JSON.encode(self[j])))
result[i++] = tmp;
};
result = "[".concat(result.join(","), "]");
break;
case Boolean:
result = String(self);
break;
56 case Date:
57 result = '"\\/Date('.concat(self.valueOf(),')\\/"');
58 break;
case Function:
break;
case Number:
result = isFinite(self) ? String(self) : "null";
break;
case String:
result = '"'.concat(self.replace(rs, s).replace(ru, u), '"');
break;
default:
var i = 0, key;
result = [];
for(key in self) {
if(self[key] !== undefined && (tmp = JSON.encode(self[key])))
result[i++] = '"'.concat(key.replace(rs, s).replace(ru, u), '":', tmp);
};
result = "{".concat(result.join(","), "}");
break;
}
};
return result;
};
this.toDate = function(){
var self = arguments.length ? arguments[0] : this,
result=self;
if(rd.test(self)){
result = new Date();
result.setHours(i(self, 11, 2));
result.setMinutes(i(self, 14, 2));
result.setSeconds(i(self, 17, 2));
result.setMonth(i(self, 5, 2) - 1);
result.setDate(i(self, 8, 2));
result.setFullYear(i(self, 0, 4));
}
92 //else if(rt.test(self))
93 //result = new Date(self * 1000);
94 else if(rr.test(self)){
95 result=new Date(self.match(/\d+/)[0]-0);
96 }
return result;
}; 100 this.defaultDateFilter=function(k,m){ // 提供通用的时间字符串解析方式(尽可能利用原有处理机制,这一过程较消耗性能,需要解析时间时使用filter)
101 switch($[typeof m](m)){
102 case Array:
103 case Object:
104 for(var p in m){
105 m[p]=v(p,m)?arguments.callee(p,m[p]):m[p];
106 }
107 return m;
108 case String:
109 return JSON.toDate(m);
110 default:
111 return m;
112 }
113 }
var c = {"\b":"b","\t":"t","\n":"n","\f":"f","\r":"r",'"':'"',"\\":"\\","/":"/"},
d = function(n){return n<10?"0".concat(n):n},
e = function(c,f,e){e=eval;delete eval;if(typeof eval==="undefined")eval=e;f=eval(""+c);eval=e;return f},
i = function(e,p,l){return 1*e.substr(p,l)},
p = ["","000","00","0",""],
rc = null,
rd = /^[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/,
rs = /(\x5c|\x2F|\x22|[\x0c-\x0d]|[\x08-\x0a])/g,
122 rr=/^\/Date\(\d+\)\/$/,
rt = /^([0-9]+|[0-9]+[,\.][0-9]{1,3})$/,
ru = /([\x00-\x07]|\x0b|[\x0e-\x1f])/g,
s = function(i,d){return "\\".concat(c[d])},
u = function(i,d){
var n=d.charCodeAt(0).toString(16);
return "\\u".concat(p[n.length],n)
},
v = function(k,v){return $[typeof v[k]](v[k])!==Function&&(v.hasOwnProperty?v.hasOwnProperty(k):v.constructor.prototype[k]!==v[k])},
$ = {
"boolean":function(){return Boolean},
"function":function(){return Function},
"number":function(){return Number},
"object":function(o){return o instanceof o.constructor?o.constructor:null},
"string":function(){return String},
"undefined":function(){return null}
},
$$ = function(m){
function $(c,t){t=c[m];delete c[m];try{e(c)}catch(z){c[m]=t;return 1}};
return $(Array)&&$(Object)
};
try{rc=new RegExp('^("(\\\\.|[^"\\\\\\n\\r])*?"|[,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t])+?$')}
catch(z){rc=/^(true|false|null|\[.*\]|\{.*\}|".*"|\d+|\d+\.\d+)$/}
};

至此,已经可以轻松搞定JSON时间处理的麻烦。不过这里有一点点不完美的地方,要求.NET端的时间使用UTC时间,总的来说这也不是什么大的缺陷,可以接受。我的理由是在多时区时就应当在服务端使用UTC时间。

让JSON.js完全适应.NET的更多相关文章

  1. Json——js和C#对Json的操作

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式.博主记得几年前在华为外包项目中有一个和Android应用交互的需求,Andr ...

  2. jQuery: jquery.json.js

    http://api.jquery.com/jQuery.parseJSON/ http://www.json.org/json-zh.html http://fineui.codeplex.com/ ...

  3. 在JavaScript中使用json.js:Ajax项目之POST请求(异步)

    经常在百度搜索框输入一部分关键词后,弹出候选关键热词.现在我们就用Ajax技术来实现这一功能. 一.下载json.js文件 百度搜一下,最好到json官网下载,安全起见. 并与新建的两个文件部署如图 ...

  4. 在JavaScript中使用json.js:Ajax项目之GET请求(同步)

    1.用php编写一个提供数据的响应程序(phpdata.php) <?php $arr=array(1,2,3,4); //将数组编码为JSON格式的数据 $jsonarr=json_encod ...

  5. 在JavaScript中使用json.js:访问JSON编码的某个值

    演示: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  6. 在JavaScript中使用json.js:使得js数组转为JSON编码

    在json的官网中下载json.js,然后在script中引入,以使用json.js提供的两个关键方法. 1.数组对象.toJSONString() 这个方法将返回一个JSON编码格式的字符串,用来表 ...

  7. prototype.js 和json.js 冲突

    1.冲突简述和分析 prototype.js与json.js并不是完全兼容的.主要冲突在于json.js为Object的原型增加了一个toJSONString的方法. 冲突之一:是prototype中 ...

  8. js便签笔记(10) - 分享:json.js源码解读笔记

    1. 如何理解“json” 首先应该意识到,json是一种数据转换格式,既然是个“格式”,就是个抽象的东西.它不是js对象,也不是字符串,它只是一种格式,一种规定而已. 这个格式规定了如何将js对象转 ...

  9. json (js对象标记)

    基础 JSON: JavaScript Object Notation (JavaScript对象表示法) 网络媒体类型是 application/json,文件名扩展是 .json JSON 独立于 ...

  10. parse XML & JSON & js

    parse XML & JSON & js how to parse xml data into json in js? https://stackoverflow.com/quest ...

随机推荐

  1. Docker数据卷

    1.volume操作命名:docker volume Usage:    docker volume COMMAND Manage Docker volumes Options:       --he ...

  2. finance1:专业词汇

    1,沙盘演练:沙盘演练又叫沙盘模拟培训.沙盘推演,源自西方军事上的战争沙盘模拟推演,是通过引领学员进入一个模拟的竞争性行业,由学员分组建立若干模拟公司,围绕形象直观的沙盘教具,实战演练模拟企业的经营管 ...

  3. git版本控制工具

    git 基本操作 1.git init 在一个文件夹下执行该命令,对该文件夹下的内容进行管理.在该文件夹下会创建一个隐藏的目录.git 2.git status 查看文件夹下内容的状态,没有更改的则什 ...

  4. excel将百分比数据转为数值格式

    由于于原给定的数据是百分比格式的, 所以先在excel中将数据格式改为数值 修改步骤: 单纯更改单元格格式为数值没用,先在空白单元格输入数值格式的1,复制该数字,选中要转换格式的数据, 右键 ---- ...

  5. 【Assembly】NO.70.EBook.7.Assembly.1.001-【汇编语言 第3版 张爽】- 基础知识

    1.0.0 Summary Tittle:[Assembly]NO.70.EBook.7.Assembly.1.001-[汇编语言 第3版 张爽]- 基础知识 Style:Assembly Serie ...

  6. 利用python脚本(re)抓取美空mm图片

    很久没有写博客了,这段时间一直在搞风控的东西,过段时间我把风控的内容整理整理发出来大家一起研究研究. 这两天抽空写了两个python爬虫脚本,一个使用re,一个使用xpath. 直接上代码——基于re ...

  7. go语言的安装与开发环境

    安装golang编译器: https://studygolang.com/dl 之后设置环境变量GOPATH(项目目录)  GOROOT(默认已经设置好) 安装编辑器:IDEA安装和破解 https: ...

  8. Cocos Creator 鼠标事件

    鼠标事件// 使用枚举类型来注册node.on(cc.Node.EventType.MOUSE_DOWN, function (event) {console.log('Mouse down');}, ...

  9. Windows平台搭建Kafka

    1. 安装JDK 1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.htm ...

  10. #WEB安全基础 : HTTP协议 | 0x8 HTTP的Cookie技术

    说道Cookie,你喜欢吃饼干吗? 这里的Cookie不是饼干=_= HTTP不对请求和响应的通信状态进行保存,所以被称为无状态协议,为了保持状态和协议功能引入了Cookie技术 Cookie技术在请 ...