HTML之JavaScript使用JSON
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。
JSON是JavaScript对象的字符串表示法,它使用文本表示一个js对象的信息,可以将json字符串转换为js对象,也可以将js对象转换为json字符串
JSON的应用场景:
1.数据交换
2.前后端数据交互 最多
3.存储数据
4.配置文件
5.服务端返回数据
6.本地存储
7.序列化和反序列化
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var personStr = '{"name":"张三","age":18,"dog":{"dname":"旺财","dage":3},"friends":["李四","王五","赵六"]}';
console.log(personStr);
console.log(typeof personStr);
console.log(personStr.name);
var personObj = JSON.parse(personStr);
console.log(personObj);
console.log(typeof personObj);
console.log(personObj.name);
var personStr1 = JSON.stringify(personObj);
console.log(personStr1);
console.log(typeof personStr1);
</script>
</head>
<body>
</body>
</html>