javascript print object as json code example
Example 1: js print object as json
var obj = {
a: 1,
b: 2,
c: { d: 3, e: 4 } // object in object
};
// method 1 - simple output
console.log(JSON.stringify(obj)); // {"a":1,"b":2,"c":{"d":3,"e":4}}
// method 2 - more readable output
console.log(JSON.stringify(obj, null, 2)); // 2 - indent spaces. Output below
/*
{
"a": 1,
"b": 2,
"c": {
"d": 3,
"e": 4
}
}
*/
Example 2: c# print object to json
var json = new JavaScriptSerializer().Serialize(ObjectModel);