Serializing object that contains object value - Javascript JSon

Javascript examples for JSon:JSon stringify

Description

Serializing object that contains object value

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {/*w w  w . j  a  v  a2 s . c om*/
obj = { x:555, y: "hi" }
obj.myself = obj
seen = []
json = JSON.stringify(obj, function(key, val) {
   if (typeof val == "object") {
        if (seen.indexOf(val) >= 0)
            return;
        seen.push(val);
    }
    return val;
})
console.log(json);
    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials