[k]输出json对象

 1 <html>

 2 <head>

 3   <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>

 4 </head>

 5 <body>

 6 <script>

 7 var object1 = {

 8   apple: 0,

 9   banana: {weight: 52, price: 100},

10   cherry: 97

11 };

12 var printObj = typeof JSON != 'undefined' ? JSON.stringify : function(obj){

13     var arr = [];

14     $.each(obj,function(key,val){

15         var next = key + ':';

16         next += $.isPlainObject(val) ? printObj(val) : val;

17         arr.push(next);

18     });

19     return '{ ' + arr + ' }';

20 }

21 alert(printObj(object1));

22 </script>

23 </body>

24 </html>

 

你可能感兴趣的:(json)