1 <!DOCTYPE html> 2 <htmllang="en"> 3 <head> 4 <metacharset="UTF-8"/> 5 <title>Document</title> 6 </head> 7 <body> 8 </body> 9 <scripttype="text/javascript"> 10 // 不符合json转换格式的字符串 11 var string ="{‘age‘:28,‘sex‘:‘boy‘}"; 12 // 用json的强制转换方法来转换这个字符串(new Function("return"+str)()); 13 var json =(newFunction("return"+string))(); 14 console.log(json.sex); 15 var li = document.createElement("li"); 16 li.innerText="name:"+json.sex +",age:"+ json.age; 17 document.body.appendChild(li); 18 </script> 19 </html>
1 <!DOCTYPE html> 2 <htmllang="en"> 3 <head> 4 <metacharset="UTF-8"/> 5 <title>转换json数据</title> 6 </head> 7 <body> 8 </body> 9 <scripttype="text/javascript"src="jquery-3.1.1.min.js"></script> 10 <scripttype="text/javascript"> 11 // 也是利用强制转换 12 data ="{root:[{name:‘1‘,value:‘0‘},{name:‘11‘,value:‘110‘},{name:‘1123‘,value:‘广东‘},{name:‘1124‘,value:‘附件‘}]}"; 13 console.log(data); 14 var json =(newFunction("return"+data))(); 15 console.log(json.root); 16 $.each(json.root,function(i,v){ 17 console.log(v); 18 var h = v; 19 for(key in h){ 20 console.log(h[key]); 21 var $span = $("<span></span><br/>"); 22 $span.text(h[key]); 23 $("body").append($span); 24 } 25 }); 26 </script> 27 </html>
原文:http://www.cnblogs.com/ChenChunChang/p/6615977.html