1. if (!window.JSON) {
    2. window.JSON = {
    3. parse: function(jsonStr) {
    4. return eval('(' + jsonStr + ')');
    5. },
    6. stringify: function(jsonObj) {
    7. var result = '',
    8. curVal;
    9. if (jsonObj === null) {
    10. return String(jsonObj);
    11. }
    12. switch (typeof jsonObj) {
    13. case 'number':
    14. case 'boolean':
    15. return String(jsonObj);
    16. case 'string':
    17. return '"' + jsonObj + '"';
    18. case 'undefined':
    19. case 'function':
    20. return undefined;
    21. }
    22. switch (Object.prototype.toString.call(jsonObj)) {
    23. case '[object Array]':
    24. result += '[';
    25. for (var i = 0, len = jsonObj.length; i < len; i++) {
    26. curVal = JSON.stringify(jsonObj[i]);
    27. result += (curVal === undefined ? null : curVal) + ",";
    28. }
    29. if (result !== '[') {
    30. result = result.slice(0, -1);
    31. }
    32. result += ']';
    33. return result;
    34. case '[object Date]':
    35. return '"' + (jsonObj.toJSON ? jsonObj.toJSON() : jsonObj.toString()) + '"';
    36. case '[object RegExp]':
    37. return "{}";
    38. case '[object Object]':
    39. result += '{';
    40. for (i in jsonObj) {
    41. if (jsonObj.hasOwnProperty(i)) {
    42. curVal = JSON.stringify(jsonObj[i]);
    43. if (curVal !== undefined) {
    44. result += '"' + i + '":' + curVal + ',';
    45. }
    46. }
    47. }
    48. if (result !== '{') {
    49. result = result.slice(0, -1);
    50. }
    51. result += '}';
    52. return result;
    53. case '[object String]':
    54. return '"' + jsonObj.toString() + '"';
    55. case '[object Number]':
    56. case '[object Boolean]':
    57. return jsonObj.toString();
    58. }
    59. }
    60. };
    61. }