JavaScript ã§ãã¬ã¼ã³ãã¼ã¿ã JSON æååã«å¤æãããµã³ãã«
Ajax ã§ã¯ã©ã¤ã¢ã³ãï¼ãµã¼ãéã JSON ã§ããåããããã£ãã®ã§ããã¬ã¼ã³ãã¼ã¿ã JSON æååã«å¤æããé¢æ°ãæ¸ãã¦ã¿ãã
ã¡ãªã¿ã« prototype.js ã«ã¯ Object ã®æ¡å¼µã¨ã㦠toJSON() ãå®ç¾©ããã¦ãããjQuery ã¯ãã©ã°ã¤ã³ã¨ãã¦åå¨ããã
ä»åã¯åä½ã®é¢æ°ã¨ãã¦å¼æ°ã«ãã¼ã¿ã渡ãããã«æ¸ãã¦ã¿ãã
function toJSON(data){ if (data === undefined) return; if (data === null) return "null"; var type = typeof data; if (type == 'number' || type == 'boolean') { return data.toString(); } else if (type == 'function' || type == 'unknown') { return; } else if (type == 'string' || data.constructor == String) { return '"' + data.replace(/\"|\n|\\/g, function(c){ return c == "\n" ? "\\n" : '\\' + c }) + '"'; } else if (data.constructor == Date) { return 'new Date("' + data.toString() + '")'; } else if (data.constructor == Array) { var items = []; for (var i = 0; i < data.length; i++) { var val = toJSON(data[i]); if (val != undefined) items.push(val); } return "[" + items.join(",") + "]"; } else if (data.constructor == Object) { var props = []; for (var k in data) { var val = toJSON(data[k]); if (val != undefined) props.push(toJSON(k) + ":" + val); } return "{" + props.join(",") + "}"; } }
Function ã¯åºåããªããDate å㯠eval ããã¨ãã« Date ãªãã¸ã§ã¯ãåããããã«ã³ã³ã¹ãã©ã¯ãã³ã¼ãã«ãªã£ã¦ãããRFC 4627 (http://www.ietf.org/rfc/rfc4627.txt) ã§ã¯æ¥ä»ã«é¢ããå®ç¾©ã¯ãªãã®ã§ããã®å¤æã¯ãµã¼ãå´ã® JSON ã¸ã§ãã¬ã¼ã¿ï¼ãã¼ãµã¼ã«åããããã
String ã®ã¨ã¹ã±ã¼ãå¦çã足ãã¦ããªãã£ãã®ãä¿®æ£ãã¾ãããï¼2008/07/09ï¼