JSON的两种核心方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
// JSON JavaScript Object Notaion,是一种前后端交互时约定的数据格式
let obj = { name: "凡凡", age: 30, height: "188" };
console.log("原对象>>>", obj);
// 1.JSON.stringify(obj) 将对象转换为对象字符串
let objStr = JSON.stringify(obj);
console.log(objStr); // '{ name: "凡凡", age: 30, height: "188" }',对象字符串,伪对象
// 2.JSON.parse(objStr) 将对象字符串转换为对象
console.log(JSON.parse(objStr)); // { name: "凡凡", age: 30, height: "188" } 真对象
</script>
</body>
</html>
推荐阅读:
扫描二维码,在手机上阅读