日期:2014-05-16  浏览次数:20397 次

JS Json对象转化成字符串对象( 例:json =[{id:1} ,{id:2}] 转化“[{id:1} ,{id:2}] ”)
/******      json =[{id:1} ,{id:2}] 转化“[{id:1} ,{id:2}] ”  *****/
jsonToString =function(obj) {
      if (obj == undefined) {
          return "";
      }
      var r = [];
      if (typeof obj == "string") return "\"" + obj.replace(/([\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
      if (typeof obj == "object") {
          if (!obj.sort) {
              for (var i in obj)
                  r.push("\"" + i + "\":" + modeler.jsonToString(obj[i]));
              if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(obj.toString)) {
                  r.push("toString:" + obj.toString.toString());
              }
              r = "{" + r.join() + "}";
          } else {
              for (var i = 0; i < obj.length; i++)
                  r.push(modeler.jsonToString(obj[i]));
              r = "[" + r.join() + "]";
          }
          return r;
      }
      return obj.toString().replace(/\"\:/g, '":""');
  } ;