ES6中对JSON扩展了新特性:
1、对象转字符串 JSON.stringify
2、字符串转对象 JSON.parse
3、key value相等可简写
4、JSON中函数方法简写
案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>JSON扩展</title>
<script>
// 1、对象转字符串
let json = {a: 1, b: 2}
console.log(JSON.stringify(json));
// 2、字符串转对象
let jsonstr = '{"a":11,"b":21}';
console.log(JSON.parse(jsonstr));
// 3、key value相等简写
let a = 11;
let b = 12
// let json2 = {a: a, b: b}
let json2 = {a, b, c: 33}
console.log(json2);
// 4、JSON中函数方法简写
let json3 = {
a: 'a1',
alertA(){
alert(this.a);
}
}
json3.alertA();
</script>
</head>
<body>
</body>
</html>
网友评论