原文: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. Orchard运用 - 为博客启用Markdown编辑器

    有时决定你是否使用某一个博客系统,最看重就是如何更简便的写博客,不能让其成为一个负担或别扭费力不讨好的工作. 对此一个好的编辑器就是一个最靓丽的卖点.比如最新的博客系统ghost.org就只定位一个最 ...

  2. TextView子类的常用属性

    TextView常见的子类包括EditText,Button,CheckBox, RadioButton等. 1.EditText EditText继承自TextView,因此TextView所有属性 ...

  3. vue - 页面跳转

    HTML:a 小程序:navigator Vue:router-link 1. router-link => a标签 2. javascript标签跳转页面 2.1   2.2 3. 常用方法之 ...

  4. 利用Lucene把文本的字体格式进行改动,然后输出到一个新的文件里

    这里书中写的是charactorProcess(File file, String destFile) 这里被我改成.(String file,  String destFIle) 一个代表现有的文件 ...

  5. Java设计模式—工厂设计模式

    工厂设计模式(减少耦合.通过接口或者工厂类来实现) 耦合性:粘度强(依耐性) Person p = new Person();  //耦合性强              Man p = new Per ...

  6. 《学习opencv》笔记——矩阵和图像操作——cvCrossProduct and cvCvtColor

    矩阵和图像的操作 (1)cvCrossProduct函数 其结构 void cvCrossProdust(//计算两个三维向量的叉积 const CvArr* src1, const CvArr* s ...

  7. php 一个过虑xxs的代码

    一个过虑xxs的代码 public static function removeXSS($str) { $str = str_replace('<!-- -->', '', $str); ...

  8. 纯css实现单行”截取“

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

  9. 转:sock_ev——linux平台socket事件框架(socket代理类) .

    前面分析了对socket基本操作的封装,并按照数据的传送方式写了两个类,本篇将写一个代理类提供给库的使用者使用的类. /**************************************** ...

  10. curl命令常用参数

    -a/--append 上传文件时,附加到目标文件 -A/--user-agent <string> 设置用户代理发送给服务器 -anyauth 可以使用“任何”身份验证方法 -b/--c ...