{"id":661,"date":"2009-02-09T14:02:06","date_gmt":"2009-02-09T06:02:06","guid":{"rendered":"http:\/\/fdream.net\/blog\/article\/661.aspx"},"modified":"2009-02-09T14:02:50","modified_gmt":"2009-02-09T06:02:50","slug":"jQuery%e6%96%b9%e6%b3%95%e6%89%a9%e5%b1%95%ef%bc%9atype%2c+toJSON%2c+evalJSON","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/661","title":{"rendered":"jQuery\u65b9\u6cd5\u6269\u5c55\uff1atype, toJSON, evalJSON"},"content":{"rendered":"<p>jQuery\u5c45\u7136\u90fd\u6ca1\u6709JSON\u7684decode\u548cencode\uff0c\u7cbe\u786e\u7c7b\u578b\u5224\u65ad\u4e5f\u6ca1\u6709\uff0c\u56e7\u2026\u2026\u81ea\u5df1\u52a8\u624b\u5199\u5427\uff01\u4e0d\u8fc7\u8fd9\u4e9b\u4e1c\u897f\u5728\u7f51\u4e0a\u90fd\u5df2\u7ecf\u6709\u5f88\u597d\u7684\u7248\u672c\u4e86\uff0c\u81ea\u5df1\u4e5f\u4e0d\u7528\u592a\u8d39\u8111\u7b4b\uff0c\u62ff\u6765\u7528\u5427\uff01\u7c7b\u578b\u5224\u65ad\u5728\u8fd9\u91cc\u6709\u4e00\u6bb5\u5f88\u597d\u7684\u4ee3\u7801\uff1a<a href=\"http:\/\/lucassmith.name\/pub\/typeof.html\" target=\"_blank\" title=\"http:\/\/lucassmith.name\/pub\/typeof.html\">http:\/\/lucassmith.name\/pub\/typeof.html<\/a>\uff0cJSON\u7684decode\u548cencode\u5c31\u76f4\u63a5\u7528Mootools\u7684\u5427\uff01\uff08\u4e0d\u8fc7Mootools\u91cc\u9762\u7684JSON.encode\u65b9\u6cd5\u8fd8\u4e0d\u591f\u5b8c\u7f8e\uff0c\u6211\u4f5c\u4e86\u4e00\u4e9b\u5b8c\u5584\u3002\uff09<\/p>\n<p><pre lang=\"javascript\">\n\/**\n* extension of JSON, type for jQuery\n* AUTHOR: xushengs@gmail.com\n* LICENSE: http:\/\/www.opensource.org\/licenses\/mit-license.php\n* WEBSITE: http:\/\/fdream.net\/\n*\/\n(function($) {\n    \/\/ the code of this function is from \n    \/\/ http:\/\/lucassmith.name\/pub\/typeof.html\n    $.type = function(o) {\n        var _toS = Object.prototype.toString;\n        var _types = {\n            'undefined': 'undefined',\n            'number': 'number',\n            'boolean': 'boolean',\n            'string': 'string',\n            '[object Function]': 'function',\n            '[object RegExp]': 'regexp',\n            '[object Array]': 'array',\n            '[object Date]': 'date',\n            '[object Error]': 'error'\n        };\n\n        return _types[typeof o] || _types[_toS.call(o)] || (o ? 'object' : 'null');\n    };\n\n    \/\/ the code of these two functions is from mootools\n    \/\/ http:\/\/mootools.net\n    var $specialChars = { '\\b': '\\\\b', '\\t': '\\\\t', '\\n': '\\\\n', '\\f': '\\\\f', '\\r': '\\\\r', '&quot;': '\\\\&quot;', '\\\\': '\\\\\\\\' };\n    var $replaceChars = function(chr) {\n        return $specialChars[chr] || '\\\\u00' + Math.floor(chr.charCodeAt() \/ 16).toString(16) + (chr.charCodeAt() % 16).toString(16);\n    };\n\n    $.toJSON = function(o) {\n        var s = [];\n        switch ($.type(o)) {\n            case 'undefined':\n                return 'undefined';\n                break;\n            case 'null':\n                return 'null';\n                break;\n            case 'number':\n            case 'boolean':\n            case 'date':\n            case 'function':\n                return o.toString();\n                break;\n            case 'string':\n                return '&quot;' + o.replace(\/[\\x00-\\x1f\\\\&quot;]\/g, $replaceChars) + '&quot;';\n                break;\n            case 'array':\n                for (var i = 0, l = o.length; i &lt; l; i++) {\n                    s.push($.toJSON(o[i]));\n                }\n                return '[' + s.join(',') + ']';\n                break;\n            case 'error':\n            case 'object':\n                for (var p in o) {\n                    s.push(p + ':' + $.toJSON(o[p]));\n                }\n                return '{' + s.join(',') + '}';\n                break;\n            default:\n                return '';\n                break;\n        }\n    };\n\n    $.evalJSON = function(s) {\n        if ($.type(s) != 'string' || !s.length) return null;\n        return eval('(' + s + ')');\n    };\n})(jQuery);\n<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>jQuery\u5c45\u7136\u90fd\u6ca1\u6709JSON\u7684decode\u548cencode\uff0c\u7cbe\u786e\u7c7b\u578b\u5224\u65ad\u4e5f\u6ca1\u6709\uff0c\u56e7\u2026\u2026\u81ea\u5df1\u52a8\u624b\u5199\u5427\uff01\u4e0d\u8fc7\u8fd9\u4e9b\u4e1c\u897f\u5728\u7f51\u4e0a\u90fd\u5df2\u7ecf\u6709\u5f88\u597d\u7684\u7248\u672c\u4e86\uff0c\u81ea\u5df1\u4e5f\u4e0d\u7528\u592a\u8d39\u8111\u7b4b\uff0c\u62ff\u6765\u7528\u5427\uff01\u7c7b\u578b\u5224\u65ad\u5728\u8fd9\u91cc\u6709\u4e00\u6bb5\u5f88\u597d\u7684\u4ee3\u7801\uff1ahttp:\/\/lucassmith.name\/pub\/typeof.html\uff0cJSON\u7684decode\u548cencode\u5c31\u76f4\u63a5\u7528Mootools\u7684\u5427\uff01\uff08\u4e0d\u8fc7Mootools\u91cc\u9762\u7684JSON.encode\u65b9\u6cd5\u8fd8\u4e0d\u591f\u5b8c\u7f8e\uff0c\u6211\u4f5c\u4e86\u4e00\u4e9b\u5b8c\u5584\u3002\uff09 \/** * extension of JSON, type for jQuery * AUTHOR: xushengs@gmail.com * LICENSE: http:\/\/www.opensource.org\/licenses\/mit-license.php * WEBSITE: http:\/\/fdream.net\/ *\/ (function($) { \/\/ the code of this function is from \/\/ http:\/\/lucassmith.name\/pub\/typeof.html $.type = function(o) { var _toS = Object.prototype.toString; var _types = { &#8216;undefined&#8217;: &#8216;undefined&#8217;, &#8216;number&#8217;: &#8216;number&#8217;, &#8216;boolean&#8217;: &#8216;boolean&#8217;, &#8216;string&#8217;: &#8216;string&#8217;, &#8216;[object Function]&#8217;: &#8216;function&#8217;, &#8216;[object &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[120,104,334],"class_list":["post-661","post","type-post","status-publish","format-standard","hentry","category-coding","tag-AJAX","tag-JavaScript","tag-jQuery"],"views":9443,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=661"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/661\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}