Convert String to Json string - Node.js String

Node.js examples for String:Convert

Description

Convert String to Json string

Demo Code

String.prototype.toJSON = function(){
  return '"' +
    this.replace(/(\\|\")/g,"\\$1")
    .replace(/\n|\r|\t/g,function(){
      var a = arguments[0];
      return  (a == '\n') ? '\\n':
          (a == '\r') ? '\\r':
          (a == '\t') ? '\\t': ""
    }) +//from   www.  j  av  a  2s.com
    '"'
}

Related Tutorials