码迷,mamicode.com
首页 > Web开发 > 详细

json 与字符串相互转换,虽然偏,面试能用到

时间:2015-05-20 18:16:38      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="智能社 - zhinengshe.com" />
<meta name="copyright" content="智能社 - zhinengshe.com" />
<title>智能社 - www.zhinengshe.com</title>
<script>

var strJson = {"username":"zns","pwd":123456,"age":18};
var strJson = "{‘username‘:‘zns‘,‘pwd‘:123456,‘age‘:18}";
// eval
console.log(typeof strJson) //string
var json = eval("("+strJson+")");//转为json

console.log(typeof json) //object

document.write(JSON.stringify(json));
var strJson = JSON.stringify(json);//json转为字符串
console.log(strJson) //{"username":"zns","pwd":123456,"age":18}
console.log(typeof strJson) //string

</script>
</head>

<body>

</body>
</html>

 

json 与字符串相互转换,虽然偏,面试能用到

标签:

原文地址:http://www.cnblogs.com/heboliufengjie/p/4517744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!