- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSON.html
More file actions
Latest commit
26 lines (25 loc) · 859 Bytes
/
Copy pathJSON.html
File metadata and controls
26 lines (25 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title></title>
<script>
varjson='{"name":"Mary","age":18,"gender":"woman"}';//js对象属性可以加双引号,也可以不加。JSON一定要加
vararr1='[1,2,3,"hello",true]';
varobj='{"arr":[1,2,3]}';//JSON对象放数组
vararr2='[{"name":"Mary",age:18,gender:"woman"}]';//JSON数组放对象
//JSON --> JS对象
varperson=JSON.parse(json);
console.log(person.name);//Mary
vararr=JSON.parse(arr1);//JSON --> JS数组
console.log(arr[3]);//hello
//JS对象 --> JSON
varjsObj={name:"Mary",age:18,gender:"woman"};
varjsonStr=JSON.stringify(jsObj);
console.log(typeof(jsonStr));//string
console.log(jsonStr);//"{"name":"Mary","age":18,"gender":"woman"}"
</script>
</head>
<body>
</body>
</html>