jQuery Data Type How to - Use JSON.stringify() function as tostring function








Question

We would like to know how to use JSON.stringify() function as tostring function.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.4.4.min.js'></script>
<script type='text/javascript'>
$(function(){<!--  w  ww.ja va2  s.  co m-->
    $("pre").text(
        JSON.stringify([1,2,3]) + "\r\n"
        + JSON.stringify([[1,2], [2,4], [3,6]]) + "\r\n"
        + JSON.stringify(23) + "\r\n"
        + JSON.stringify('hello world') + "\r\n"
        + JSON.stringify({'one': 1, 'two': 2, 'three': 3})
    );
});
</script>
</head>
<body>
  <pre></pre>
</body>
</html>

The code above is rendered as follows: