23_JSON.html 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // JSON 常用于前后端数据交互 本质上就是字符串 对象格式的字符串
  11. // let person = {
  12. // name:"张三",
  13. // age:18
  14. // }
  15. // // 将对象转换为 JSON 字符串
  16. // let personJson = `{"name":"张三","age":18}`;
  17. // console.log(personJson);
  18. let person = {
  19. name:"张三",
  20. age:18,
  21. sex:"男",
  22. address:"中国",
  23. phone:"13800000000",
  24. email:"zhangsan@example.com",
  25. isSingle:true,
  26. hobbies:["篮球","足球","跑步"]
  27. }
  28. // 将对象转换为 JSON 字符串 JSON.stringify();
  29. let personJson = JSON.stringify(person);
  30. console.log(personJson);
  31. // console.log(person);
  32. // 将 JSON字符串转换为对象 JSON.parse();
  33. let personObj = JSON.parse(personJson);
  34. console.log(personObj);
  35. </script>
  36. </body>
  37. </html>