<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
//string转为obj json 方法一 ==============
var str1 = ‘{"name":"jieke","sex":"boy","age":"11"}‘;
var str2 = ‘{name:"jieke",sex:"boy","age":"26"}‘;
//alert(typeof str1);
var obj1 = eval(‘(‘+ str1 +‘)‘); //OK
var obj2 = eval(‘(‘+ str2 +‘)‘); //OK
alert(obj2.age);
//string转为obj json 方法二==============
var obj3 = jQuery.parseJSON(str1); //jQuery自带语法对语法要求严格
//var obj4 = jQuery.parseJSON(str2); //对语法要求严格报错
alert( obj3.name );
</script>
时间: 2024-10-25 23:39:30