9_JSON.html 893 B

123456789101112131415161718192021222324252627282930
  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. let person = {
  11. name: '张三',
  12. age: 18,
  13. sex: '男',
  14. school:"黑大"
  15. }
  16. // JSON.stringify 可以将对象类型转换为JSON字符串
  17. var jsonRes = JSON.stringify(person);
  18. console.log(jsonRes);
  19. // JSON字符串 常用作于数据传递 比如 后端向前端提供数据
  20. let jsonStr = '{"name":"张三","age":18,"sex":"男","school":"黑大"}';
  21. // JSON.parse 可以将JSON字符串转换为对象类型
  22. let jsonObj = JSON.parse(jsonStr);
  23. console.log(jsonObj);
  24. // let [a,b,c,d] = jsonStr;
  25. // console.log(a,b,c,d);
  26. </script>
  27. </body>
  28. </html>