1.ES5下的json
1.1 基本概念
- 是对象
- 简写形式,名字跟值(key和value)一样,留一个就行
- 方法 :function一块删 即show:function(){...}等价于show(){}
1.2 JSON->字符串
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON->字符串</title>
<script>
let json = {
"a": 11,
show() {
alert(this.a);
}
};
json.show();
</script>
</head>
<body>
</body>
</html>
2.ES6下的json
2.1 基本概念
- 标准写法:只能用双引号,所有的名字都必须用引号包起来
2.2 字符串->JSON
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>字符串->JSON</title>
<script>
let str = '{ "a": 11,"b": 21}';
let json = JSON.parse(str);
alert(json);
console.log(json);
</script>
</head>
<body>
</body>
</html>
2.3 JSON->字符串
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON->字符串</title>
<script>
let json = {
"a": 11,
"b": 21
};
let str = JSON.stringify(json);
alert(str);
</script>
</head>
<body>
</body>
</html>