javascript、java和json对象互转

1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4 <title>Insert title here</title> 5 <script type="text/javascript" src="js/prototype-1.6.0.3.js"></script> 6 <script type="text/javascript">        //字符串转换为json对象
      var str = \'{"name":"tom","age":25}\';
        var json = eval("("+str+")");


7 f2(); 8 //以json格式创建js对象 9 function f1(){ 10 var person = {\'name\':\'zs\',\'age\':22}; 11 //alert(person.name); 12 var person2 = {\'name\':\'ls\',\'address\':{ 13 \'city\':\'beijing\', 14 \'room\':2002 15 }}; 16 //alert(person2.address.city); 17 var family = [{\'name\':\'zs\',\'age\':22}, 18 {\'name\':\'ww\',\'age\':32}]; 19 alert(family[0].age); 20 } 21 22 //怎样将一个符合json格式的字符串转变成js对象 23 function f2(){ 24 var personDesc = "{\'name\':\'zs\',\'age\':22}"; 25 //alert(typeof personDesc); 26 var person = personDesc.evalJSON(); 27 //alert(person.age); 28 var familyDesc="[{\'name\':\'zs\',\'age\':22},{\'name\':\'ww\',\'age\':32}]"; 29 var family = familyDesc.evalJSON(); 30 alert(family[0].name); 31 } 32 </script> 33 </head> 34 <body style="font-size:30pt;"> 35 36 </body> 37 </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzzyxs.html