原文: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的更多相关文章

  1. 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 ...

  2. JsonUtil

    package com.test.base.util.json; import java.beans.IntrospectionException; import java.beans.Introsp ...

  3. 构造Json对象串工具类

    import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.Property ...

  4. JSON入门教程

    尽管有许多宣传关于 XML 如何拥有跨平台,跨语言的优势,然而,除非应用于 Web Services,否则,在普通的 Web 应用中,开发者经常为 XML 的解析伤透了脑筋,无论是服务器端生成或处理 ...

  5. [转]fastjson

    原文地址:http://www.cnblogs.com/zhenmingliu/archive/2011/12/29/2305775.html FastJSON是一个很好的java开源json工具类库 ...

  6. [转]JSON 入门指南

    原文地址:http://www.ibm.com/developerworks/cn/web/wa-lo-json/ 尽管有许多宣传关于 XML 如何拥有跨平台,跨语言的优势,然而,除非应用于 Web ...

  7. Json Utils

    import java.util.List;import java.util.Map; import net.sf.json.JSONArray;import net.sf.json.JSONObje ...

  8. FastJSON 简介及其Map/JSON/String 互转

    在日志解析,前后端数据传输交互中,经常会遇到 String 与 map.json.xml 等格式相互转换与解析的场景,其中 json 基本成为了跨语言.跨前后端的事实上的标准数据交互格式.应该来说各个 ...

  9. Java将其他数据格式转换成json字符串格式

    package com.wangbo.util; import java.beans.IntrospectionException; import java.beans.Introspector; i ...

随机推荐

  1. 使用C# + httpWebRequest 解析WMTS服务元数据

    解析http://219.142.81.86/igserver/ogc/kvp/TAS10R52000J49/WMTSServer服务的元数据 // 测试httpWebRequest读取 wmts元数 ...

  2. jQuery数组处理全解

    jQuery的数组处理.便捷.功能齐全.最近的项目中用到的比较多,深感实用,一步到位的封装了很多原生JavaScript数组不能企及的功能.最近时间紧迫,今天抽了些时间回过头来看jQuery中文文档中 ...

  3. 13个 ASP.NET MVC 的扩展

    ASP.NET MVC设计的主要原则之一是可扩展性.处理管线(processing pipeline)上的所有(或大多数)东西都是可替换的.因此,如果您不喜欢ASP.NET MVC所使用的约定(或缺乏 ...

  4. 查看网络IP连接

  5. Understanding the JavaScript Engine—— two phase

    Understanding the JavaScript Engine — Part 1   I have been a Ruby on Rails developer for the last 2 ...

  6. 微软小冰你这么智能 .net知道吗?

  7. [Compose] 9. Delay Evaluation with LazyBox

    We rewrite the Box example using lazy evaulation. Here is Box example: const Box = (x) => ({ map: ...

  8. Vc++内存布局

    Vc++内存布局 测试平台 Windows server 2012 R2 and visual studio 2013 professional. 本篇文章意在介绍vc++中类的内存布局方式,只是研究 ...

  9. 【python】如何安装BeautifulSoup4

    在cmd窗口输入 pip install beautifulsoup4,如下: C:\Users\horn1\Desktop\python\3>pip install beautifulsoup ...

  10. android 开源组件合集-UI篇(2013-11-07更新)

    其实也算不上合集,只是将我经常用到的部分整理一下,如果您有好东西,也可以留言补充 1.actionbar http://actionbarsherlock.com/ https://github.co ...