动机:.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. php-memcache基本用法

    //create a memcache object        $mem = new Memcache(); //create connection        $mem->connect ...

  2. luogu2839 [国家集训队]middle

    题目链接:洛谷 题目大意:给定一个长度为$n$的序列,每次询问左端点在$[a,b]$,右端点在$[c,d]$的所有子区间的中位数的最大值.(强制在线) 这里的中位数定义为,对于一个长度为$n$的序列排 ...

  3. 集合 & 深浅copy

    集合: 特点:集合是可变的数据类型,但他里面的元素必须是不可变的数据类型,无序,不可重复. 创建: set1 = set({1,2,3}) 或者直接创建set2 = {1,2,3} 集合的增删查: 增 ...

  4. HTTP状态码简单总结

    状态码数字的第一位表明了响应类别,如以下5种:     类别 原因短语 1XX Informational(信息性状态码) 接受的请求正在处理 2XX Success(成功状态码) 请求正常处理完毕 ...

  5. 菜鸟手下的iOS开发笔记(swift)

    在阳春4月的一天晨会上,有一个老板和蔼的对他的一个菜鸟手下说:“你既然会Android,那你能不能开发iOS?” 不是说好的要外包的吗?内心跌宕,但是表面淡定的菜鸟手下弱弱的回道:“可以试试”. 第二 ...

  6. RabbitMQ:Docker环境下搭建rabbitmq集群

    RabbitMQ作为专业级消息队列:如何在微服务框架下搭建 使用组件 文档: https://github.com/bijukunjummen/docker-rabbitmq-cluster 下载镜像 ...

  7. python调用RPC接口

    要调用RPC接口,python提供了一个框架grpc,这是google开源的 rpc相关文档: https://grpc.io/docs/tutorials/basic/python.html 需要安 ...

  8. windows程序设计 MessageBox消息框

    MessageBox函数 int WINAPI MessageBoxW( HWND hWnd,//窗口句柄 LPCWSTR lpText,//消息框主体显示的字符串 LPCWSTR lpCaption ...

  9. sublime-text3打造markdown编辑器

    编辑插件 sublime自带的markdown语法高亮并不是很友好,推荐安装Markdown Editing,github主页然后在视图->语法里选择MarkdownEditing启用,支持三种 ...

  10. 玩转spring boot——结合docker

    前言 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 liunx机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有 ...