复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="Nancle from CAU CS 101" />
<title>JSON序列化与解析(原生JS + Object对象拓展方法)【IE6和chrome测试通过】</title>
</head>
<script type="text/javascript">
var ele = {
x:11,
y:'string',
z:{x:11, y:'string'}
}
toJSON = function(obj){
var arr = [];
for(var key in obj){
var value = obj[key];
if(value == null){
value = '';
}else{
value = (typeof value === 'string' | typeof value === 'number')
? ('"' + value + '"') : toJSON(value);
}
var str = '"' + key + '":' + value;
arr.push(str);
}
return '{' + arr.join(',') + '}';
}
var str = toJSON(ele);
alert('装换成的字符串是: ' + str );
var ele2 = eval('(' + str + ')');
alert('解析字符串得到js对象: x=' + ele2.x + ',y=' + ele2.y + ',z=' + ele2.z);
</script>
<body>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="Nancle from CAU CS 101" />
<title>JSON序列化与解析(原生JS + Object对象拓展方法)【IE6和chrome测试通过】</title>
</head>
<script type="text/javascript">
var ele = {
x:11,
y:'string',
z:{x:11, y:'string'}
}
toJSON = function(obj){
var arr = [];
for(var key in obj){
var value = obj[key];
if(value == null){
value = '';
}else{
value = (typeof value === 'string' | typeof value === 'number')
? ('"' + value + '"') : toJSON(value);
}
var str = '"' + key + '":' + value;
arr.push(str);
}
return '{' + arr.join(',') + '}';
}
var str = toJSON(ele);
alert('装换成的字符串是: ' + str );
var ele2 = eval('(' + str + ')');
alert('解析字符串得到js对象: x=' + ele2.x + ',y=' + ele2.y + ',z=' + ele2.z);
</script>
<body>
</body>
</html>
标签:
JSON序列化,JSON解析
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“JSON序列化与解析原生JS方法且IE6和chrome测试通过”评论...