js中 JSON和string转换的方式

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>JSON和string类型之间相互转换</title>
		<script type="text/javascript">
			var obj = {name:"张三" , age:28 , gender:"男"};
			// console.log(obj)
			var str = JSON.stringify(obj);
			// console.log(str) // {"name":"张三","age":28,"gender":"男"}
			var strJson = '{"name":"张三","age":28,"gender":"男"}';
			// console.log(strJson)
			//方式1
			obj = JSON.parse(strJson);
			console.log(obj.age)
			//方式2
			obj = eval("("+strJson+")"); 
			console.log(obj.name)
		</script>
	</head>
	<body>
	</body>
</html>

你可能感兴趣的:(#,JavaScript,从入门到放弃)