json 的小知识点:字符串--->javascript
字符串转化成javascript对象
var json="{name:'ygl',age:18}";
1.var data=eval("("+json+")");
是由于{}在javascript中有多种含义;
2.data=(new Function("","return"+json))();
alert(data.name);
//================================例子:new Function F要大写
<html>
<head>
</head>
<body>
<script type="text/javascript">
window.onload=function (){
var json="{name:'ygl',age:18}";
// var data=eval("("+json+")");
//alert(data.name);
var data=(new Function("","return "+json))();
alert(data.name);
var json1='{"name":"ygl","age":18}';//这里属性也需要用""
var data=JSON.parse(json1);//IE8 或者firefox 。。。用JSON
// alert(data.name)
//对象 转成 字符串(可能乱码,因为他是GBK,)
var a={
t:1,
n:"白金",
id:"A2",
own:"A1"
};
alert(JSON.stringify(a));
//{"t":1,"n":"白金","id":"A2","own":"A1"}
}
}
</script>
</body>
</html>