| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- <!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>
- // JSON 常用于前后端数据交互 本质上就是字符串 对象格式的字符串
- // let person = {
- // name:"张三",
- // age:18
- // }
- // // 将对象转换为 JSON 字符串
- // let personJson = `{"name":"张三","age":18}`;
- // console.log(personJson);
- let person = {
- name:"张三",
- age:18,
- sex:"男",
- address:"中国",
- phone:"13800000000",
- email:"zhangsan@example.com",
- isSingle:true,
- hobbies:["篮球","足球","跑步"]
- }
- // 将对象转换为 JSON 字符串 JSON.stringify();
- let personJson = JSON.stringify(person);
- console.log(personJson);
- // console.log(person);
- // 将 JSON字符串转换为对象 JSON.parse();
- let personObj = JSON.parse(personJson);
- console.log(personObj);
-
- </script>
- </body>
- </html>
|