Add method to Object to return json String and deal with null value - Node.js String

Node.js examples for String:String Value

Description

Add method to Object to return json String and deal with null value

Demo Code

Object.prototype.toJSON = function(){
  var json = [];/*from  www.  ja  v  a  2 s . c o m*/
  for(var i in this){
    if(!this.hasOwnProperty(i)) continue;
    //if(typeof this[i] == "function") continue;
    json.push(
      i.toJSON() + " : " +
      ((this[i] != null) ? this[i].toJSON() : "null")
    )
  }
  return "{\n " + json.join(",\n ") + "\n}";
}

Related Tutorials