123456789101112131415161718192021222324252627282930 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- let person = {
- name: '张三',
- age: 18,
- sex: '男',
- school:"黑大"
- }
- // JSON.stringify 可以将对象类型转换为JSON字符串
- var jsonRes = JSON.stringify(person);
- console.log(jsonRes);
-
- // JSON字符串 常用作于数据传递 比如 后端向前端提供数据
- let jsonStr = '{"name":"张三","age":18,"sex":"男","school":"黑大"}';
- // JSON.parse 可以将JSON字符串转换为对象类型
- let jsonObj = JSON.parse(jsonStr);
- console.log(jsonObj);
- // let [a,b,c,d] = jsonStr;
- // console.log(a,b,c,d);
- </script>
- </body>
- </html>
|