array2json
原文:jQuery方法扩展:type, toJSON, evalJSON. http://zhkac.iteye.com/blog/499330 .2013-05-19
(function($) {
// the code of this function is from
// http://lucassmith.name/pub/typeof.html
$.type = function(o) {
var _toS = Object.prototype.toString;
var _types = {
'undefined': 'undefined',
'number': 'number',
'boolean': 'boolean',
'string': 'string',
'[object Function]': 'function',
'[object RegExp]': 'regexp',
'[object Array]': 'array',
'[object Date]': 'date',
'[object Error]': 'error'
};
return _types[typeof o] || _types[_toS.call(o)] || (o ? 'object' : 'null');
};
// the code of these two functions is from mootools
// http://mootools.net
var $specialChars = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"': '\\"', '\\': '\\\\' };
var $replaceChars = function(chr) {
return $specialChars[chr] || '\\u00' + Math.floor(chr.charCodeAt() / 16).toString(16) + (chr.charCodeAt() % 16).toString(16);
};
$.toJSON = function(o) {
var s = [];
switch ($.type(o)) {
case 'undefined':
return 'undefined';
break;
case 'null':
return 'null';
break;
case 'number':
case 'boolean':
case 'date':
case 'function':
return o.toString();
break;
case 'string':
return '"' + o.replace(/[\x00-\x1f\\"]/g, $replaceChars) + '"';
break;
case 'array':
for (var i = 0, l = o.length; i < l; i++) {
s.push($.toJSON(o[i]));
}
return '[' + s.join(',') + ']';
break;
case 'error':
case 'object':
for (var p in o) {
s.push(p + ':' + $.toJSON(o[p]));
}
return '{' + s.join(',') + '}';
break;
default:
return '';
break;
}
};
$.evalJSON = function(s) {
if ($.type(s) != 'string' || !s.length) return null;
return eval('(' + s + ')');
};
})(jQuery);
array2json的更多相关文章
- array2json() - Convert PHP arrays to JSON
array2json is a PHP function that will convert the array given as its argument into a JSON string. T ...
- JsonUtil
package com.test.base.util.json; import java.beans.IntrospectionException; import java.beans.Introsp ...
- 构造Json对象串工具类
import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.Property ...
- JSON入门教程
尽管有许多宣传关于 XML 如何拥有跨平台,跨语言的优势,然而,除非应用于 Web Services,否则,在普通的 Web 应用中,开发者经常为 XML 的解析伤透了脑筋,无论是服务器端生成或处理 ...
- [转]fastjson
原文地址:http://www.cnblogs.com/zhenmingliu/archive/2011/12/29/2305775.html FastJSON是一个很好的java开源json工具类库 ...
- [转]JSON 入门指南
原文地址:http://www.ibm.com/developerworks/cn/web/wa-lo-json/ 尽管有许多宣传关于 XML 如何拥有跨平台,跨语言的优势,然而,除非应用于 Web ...
- Json Utils
import java.util.List;import java.util.Map; import net.sf.json.JSONArray;import net.sf.json.JSONObje ...
- FastJSON 简介及其Map/JSON/String 互转
在日志解析,前后端数据传输交互中,经常会遇到 String 与 map.json.xml 等格式相互转换与解析的场景,其中 json 基本成为了跨语言.跨前后端的事实上的标准数据交互格式.应该来说各个 ...
- Java将其他数据格式转换成json字符串格式
package com.wangbo.util; import java.beans.IntrospectionException; import java.beans.Introspector; i ...
随机推荐
- 用curl抓取网站数据,仿造IP、防屏蔽终极强悍解决方式
最近在做一些抓取其它网站数据的工作,当然别人不会乖乖免费给你抓数据的,有各种防抓取的方法.不过道高一尺,魔高一丈,通过研究都是有漏洞可以钻的.下面的例子都是用PHP写的,不会用PHP来curl的孩纸先 ...
- 使用Intellij加载Spark源代码
如何使用Intellij加载Spark源代码 转载注明原文http://www.cnblogs.com/shenh062326/p/6189643.html 查看Spark源代码或修改Spark源代码 ...
- 关于loader加载的东西必须是继承sprite
如果不是继承sprite
- 理解JAVASCRIPT 中hasOwnProperty()和isPrototypeOf的作用
hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象.不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员.格式如下: 1. 示例一: ...
- mysql查找数据库中是否已经存在某张表
Sql: select count(*) from information_schema.TABLES t where t.TABLE_SCHEMA ="数据库名" and t.T ...
- 修复错误配置/etc/fstab文件导致系统无法正常启动
1.文件介绍 /etc/fstab这个文件描述系统中各种文件系统的信息,应用程序读取这个文件,然后根据其内容进行自动挂载的工作.作为系统配置文件,fstab通常都位于/etc目录下,它包括了所有分 ...
- 关于UbuntuMate的两个问题点:SSH问题处理与自启动项配置
一.SSH连接报错问题 ssh到某台机器时候,存在如下报错: /usr/bin/xauth: timeout in locking authority file /home/sam/.Xauthori ...
- OpenMeetings(4)----新用户注册
用户登录与注册的主要代码都在WebContent\src\base\auth\checkLoginData.lzx文件中 <simpleLabelButton labelid=" ...
- 查找文本工具grep
许多时候需要从一大堆的命令输出或文本内容中找出一两行关键的内容,例如从系统用户文件中查找某个用户.如果不借助工具,这将是一项非常繁琐的工作,这时可以使用grep工具对内容进行筛选. grep(glob ...
- Python 遍历set
遍历set 由于 set 也是一个集合,所以,遍历 set 和遍历 list 类似,都可以通过 for 循环实现. 直接使用 for 循环可以遍历 set 的元素: weekdays = set([' ...